Modified logon control script



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: 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: login and logoff timestamps
    ... I just added example logoff, startup, and shutdown scripts to the page with ... the example logon script that logs information to a shared log file. ...
    (microsoft.public.windows.server.active_directory)
  • Log File!
    ... I'm trying to read the logon and the logoff time from a log file. ... order to determine the total time of the user since the first logon, ... Can I write a script to read ...
    (microsoft.public.win2000.group_policy)
  • Log File!
    ... I'm trying to read the logon and the logoff time from a log file. ... order to determine the total time of the user since the first logon, ... Can I write a script to read ...
    (microsoft.public.internet.radius)
  • Log File!
    ... I'm trying to read the logon and the logoff time from a log file. ... order to determine the total time of the user since the first logon, ... Can I write a script to read ...
    (microsoft.public.windows.server.active_directory)