dim bFirstResults, bLastResults, wshShell
dim oWMI, cProcesses
bFirstResults = vbFalse
bLaterResults = vbTrue
'First pass - we do this on the first login to make sure the user has a chance to get logged in.
'The first loop runs until it sees wfica32.exe
'The second loop runs until it *doesn't* see wfica32.exe
Do Until bFirstResults = vbTrue
wscript.sleep(10000)
bFirstResults = FirstPass()
Loop
Do
WScript.Sleep(5000)
bLaterResults = LaterPass()
Loop Until bLaterResults = vbFalse
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "logoff.exe", 0, vbFalse
Function FirstPass( )
Set oWMI= GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set cProcesses = oWMI.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'notepad.exe'")
If cProcesses.Count=0 Then
bFirstResults = vbFalse
else
bFirstResults = vbTrue
End if
FirstPass = bFirstResults
End Function
Function LaterPass( )
Set oWMI= GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set cProcesses = oWMI.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'notepad.exe'")
If cProcesses.Count=0 Then
bLaterResults = vbFalse
Else
bLaterResults = vbTrue
End if
wscript.echo "Later Pass"
LaterPass = bLaterResults
End Function
Now, an explanation of how this works.
bFirstResults = vbFalse
If we didn't give them that, they would be stuck in an endless reboot loop, because they couldn't get logged in quickly enough. With this in mind, the script checks every 10 seconds to see if the wfica32.exe is running (this is Citrix's runtime executable for live ICA connections). If the user has not logged in, it will continue to loop and check every 10 seconds to see if it is there. By doing this, we are preventing needless reboot cycles.
bLaterResults = vbTrue
and when the connection fails, we will log them out of the terminal. The script is configured to check every 5 seconds, but this can be tuned to handle your environment. Once the check fails, we move on to the next line of the script which runs logoff.exe. With no switches, this automatically logs the user out.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Author
Commented:Coralon