Re: Modified logon control script
- From: "Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 27 Jan 2006 18:06:19 -0600
Hi,
Also, your use of the InStr function is incorrect. You have:
If Not InStr(lastlog,sComputer) Then 'Check if it's the same pc
The InStr function returns the first position in the string lastlog where
the string sComputer appears. If the string sComputer appears in lastlog
(the first line of the file, which is the last log entry), then InStr
returns an integer greater than 0 (such as 18). However, when you Not the
integer 18 you do not get zero, but -19, which is still True (anything other
than 0 is True). This is subtle, but the snippet below demonstrates:
lastlog = "This computer is west012"
sComputer = "west012"
Wscript.Echo "Value: " & InStr(lastlog, sComputer)
Wscript.Echo "Not of Value: " & (Not InStr(lastlog, sComputer))
Wscript.Echo "Boolean Result: " & CBool(Not InStr(lastlog, sComputer))
Of course, if the string sComputer does not appear in the string lastlog,
the InStr function returns 0, the Not of which is -1, which is also True.
I would suggest using:
If (InStr(lastlog, sComputer) = 0) Then
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
"Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:OUnJhB0IGHA.3816@xxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> Interesting idea. I would suggest running the script at a command prompt
> after logon to troubleshoot. It's hard for us to tell with word wrapping.
>
> A few points. First, if the "old" computer is not online, connecting with
> WMI will raise an error. You will need to trap the possible error. Next,
> you search for the string "at" in the line to find the computer name. This
> could easily be part of the user name (like administrator for example).
> Either search for " at " or use some delimiter between fields that will
> never appear in the time, username, or computer names. I would suggest the
> semicolon ";". You'll have to adjust how you parse for the machine name.
> If you use " at ", then you can use:
>
> i = InStr(lastlog, " at ")
> oldComputer = Right(lastlog, Len(lastlog) -i -4)
>
> For the error trapping, something like this:
>
> On Error Resume Next
> Set objWMIService = GetObject("winmgmts:" &
> "{impersonationLevel=impersonate}!\\" & oldComputer & "\root\cimv2")
> If (Err.Number = 0) Then
> ' oldComputer found.
> On Error GoTo 0
> Set colComputer = objWMIService.ExecQuery("Select * from
> Win32_ComputerSystem")
> 'Get the logon user on the older computer
> For Each objComputer in colComputer
> oldUserName = objComputer.UserName
> Next
> Else
> On Error GoTo 0
> ' oldComputer offline, no user.
> oldUserName = ""
> End If
>
> --
> Richard
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
>
> "TomKing" <TomKing@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:60D7310C-AA9B-44C0-8696-B8447DA643B2@xxxxxxxxxxxxxxxx
>> Following script is used as a logon script. When user logon it will check
>> and
>> write info into a log file. If user has log as "logon" another computer,
>> it
>> will auto logoff that user. It was working before, until I did some
>> changes
>> to it. The whole script stop working and didn't give any error message. I
>> just add a checking condition, check if user still logon the old computer
>> or
>> not.
>>
>> Option Explicit
>> Dim oNet, sUser, sComputer, ServerLog
>>
>> 'Get current UserName and ComputerName
>> ServerLog = "\\server\logfilesFolder$\"
>> Set oNet = CreateObject("Wscript.Network")
>> sUser = oNet.UserName
>> sComputer = oNet.ComputerName
>> Set oNet = Nothing
>>
>> Dim fso, f1, WshShell, argu, alllog, lastlog
>> Dim FileName
>> FileName=ServerLog & sUser & ".txt"
>> Set fso = CreateObject("Scripting.FileSystemObject")
>> 'If the file not exist, create the log file
>> If Not (fso.FileExists(FileName)) Then
>> Set f1 = fso.CreateTextFile(FileName,True)
>> f1.WriteLine sUser & " " & sComputer
>> f1.Close
>> Set WshShell = Wscript.CreateObject("Wscript.Shell")
>> argu = FileName & "/T/E/G" & sUser & ":f /R Everyone"
>> WshShell.run("cacls " & argu)
>> Set WshShell = Nothing
>> End If
>>
>> 'Read out all contents
>> Set f1 = fso.OpenTextFile(FileName, 1, True)
>> alllog = f1.readall
>> f1.Close
>> 'Read out the first line (last log)
>> Set f1 = fso.OpenTextFile(FileName, 1, True)
>> lastlog = f1.readline
>> f1.Close
>>
>> If Left(lastlog, 5) = "logon" Then 'Check if already logon
>> If Not InStr(lastlog,sComputer) Then 'Check if it's the same pc
>> '=========================
>> 'Get the old computer name
>> Dim i,oldComputer,oldUserName
>> Dim objWMIService,colComputer,objComputer
>> i=InStr(lastlog,"at")
>> oldComputer=Right(lastlog,Len(lastlog)-i-2)
>> Set objWMIService = GetObject("winmgmts:" &
>> "{impersonationLevel=impersonate}!\\" & oldComputer & "\root\cimv2")
>> Set colComputer = objWMIService.ExecQuery("Select * from
>> Win32_ComputerSystem")
>> 'Get the logon user on the older computer
>> For Each objComputer in colComputer
>> oldUserName = objComputer.UserName
>> Next
>> '===================================
>> 'Check if the user is still on that old computer
>> If InStr(oldUserName,sUser) Then
>> 'Open the txt file and write log fail
>> Set f1 = fso.OpenTextFile(FileName, 2, True)
>> f1.WriteLine (lastlog & Chr(13) & Chr(10) & "fail-so-logoff:" & Now() & "
>> " & sUser & " at " & scomputer & Chr(13) & Chr(10) & alllog)
>> f1.Close
>> 'Give user Warning message
>> Set WshShell = Wscript.CreateObject("Wscript.Shell")
>> WshShell.popup "Sorry, this account has already logon " & oldComputer &
>> "! If you are the owner of this account contact IT helpdesk immediately,
>> thanks.", 30
>> Set WshShell = Nothing
>> Dim os, retcode
>> 'Logoff the user
>> For Each os In
>> GetObject("Winmgmts:{impersonationLevel=impersonate,(shutdown,remoteshutdown)}!//"
>> + sComputer).InstancesOf("Win32_OperatingSystem")
>> retcode = os.Win32ShutDown(4, 0)
>> Next
>> Wscript.quit
>> Else
>> Set f1 = fso.OpenTextFile(FileName, 2, True)
>> f1.WriteLine ("logon: " & Now() & " " & sUser & " at " & sComputer &
>> Chr(13) & Chr(10) & alllog)
>> f1.Close
>> End If
>> End If
>> Else 'Hasn't logon
>> Set f1 = fso.OpenTextFile(FileName, 2, True)
>> f1.WriteLine ("logon: " & Now() & " " & sUser & " at " & sComputer &
>> Chr(13) & Chr(10) & alllog)
>> f1.Close
>> End If
>> Set f1 = Nothing
>> Set fso = Nothing
>> Wscript.quit
>
>
.
- References:
- Modified logon control script
- From: TomKing
- Re: Modified logon control script
- From: Richard Mueller
- Modified logon control script
- Prev by Date: Re: Robocopy Question?
- Next by Date: Re: Leading 0 when displaying month and date if it is a single digit
- Previous by thread: Re: Modified logon control script
- Next by thread: Re: Interactive Windows form modification, is it possible with MSH?
- Index(es):
Relevant Pages
|