Modified logon control script
- From: "TomKing" <TomKing@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 26 Jan 2006 23:08:02 -0800
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
.
- Follow-Ups:
- Re: Modified logon control script
- From: Richard Mueller
- Re: Modified logon control script
- Prev by Date: Re: Scripting directory/file security in IIS Manager?
- Next by Date: Re: [MSH] Bringing form to front
- Previous by thread: Re: Change the color of the Text in a Shell.popup window
- Next by thread: Re: Modified logon control script
- Index(es):
Relevant Pages
|