Re: Modified logon control script
- From: "Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 27 Jan 2006 06:51:20 -0600
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
.
- Follow-Ups:
- Re: Modified logon control script
- From: Richard Mueller
- Re: Modified logon control script
- References:
- Modified logon control script
- From: TomKing
- Modified logon control script
- Prev by Date: Re: [MSH] Bringing form to front
- Next by Date: Re: Avoiding output message through [void]
- Previous by thread: Modified logon control script
- Next by thread: Re: Modified logon control script
- Index(es):
Relevant Pages
|