Re: Modified logon control script



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


.



Relevant Pages

  • Re: Modified logon control script
    ... your use of the InStr function is incorrect. ... I would suggest running the script at a command prompt ... > after logon to troubleshoot. ... >> Dim fso, f1, WshShell, argu, alllog, lastlog ...
    (microsoft.public.windows.server.scripting)
  • Re: track time of employee log on and log off
    ... You can turn on "Account Logon Auditing" and to write a script ... date/time to a log file. ... A similar logoff script can log similar information. ...
    (microsoft.public.win2000.active_directory)
  • Re: Users last logon info from logon script
    ... I put this line in the users logon script: ... If I run the script manually, it shows the last logon time. ... I guess it is because Windows update the lastlogon attribute once a user ... Then, for each Domain Controller, ADO is used to search the ...
    (microsoft.public.security)
  • Re: slow logon
    ... in the logon time have been reduced so far. ... update the time for the clients that talk to the dc as the logon server. ... start up script in AD to apply updates to the clients machines which is ...
    (microsoft.public.windows.server.active_directory)
  • Logon Script Causing Laptops To Hang - Problems in script?
    ... I'm using the following script to map drives, ... functions when users logon to our domain. ... 'Disconnects Drives This assures everyone has the same drive mappings. ... objNetwork.MapNetworkDrive strTrainDrv, strPath ...
    (microsoft.public.scripting.vbscript)