Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Logoff Script

HTVi
TV Discussion Newsgroups

Logoff Script
camille.reeves[ at ]gmail.com 6/27/2007 1:13:32 PM
I'm trying to write a script that will check and see if a particular
program is running on a computer (in my case VMware Workstation)
whenever someone tries to logoff and prevent the logoff from
happening
if found. In the past people have logged off computers while VMware
was running and usually these virtual machines would be running QE
tests at the time so it would always be a real pain when they were
shut off without warning. I want my script to check for one of the
virtual machine's executable files (vmware-vmx.exe) and, if found,
prompt a warning to the user that says something to the effect of
"logoff canceled, VMs are currently running on this computer" then
prevent the logoff process from happening. Otherwise, if the
executible isn't found, let the user logoff.

I've edited group policy so that logoff isn't displayed in the start
menu or in Ctrl+Alt+Del which forces all users to run my script if
they want to logoff. The only problem is my script doesn't work.
When
I click on it, it logs me off regardless of whether or not a virtual
machine is running. I can't figure out why. I would appreciate some
help and feedback.


' Require all variables to be declared before they can be used.
' This helps with debugging issues
Option Explicit

' Define all of the variables
Dim strComputer, objWMIService, colProcesses, objProcess, strList

' Seting strComputer to "." connects to the local machine.
strComputer = "."

' Connect to the Windows Management Instruments interface
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
' the impersonate line is for security clearance
' in order to query another machine's hardware

' Get a collection of all running processes
Set colProcesses = objWMIService.ExecQuery("Select * FROM
Win32_Process")

' Cycle through every currently running process
For Each objProcess in colProcesses
'outputs process list
strList = strList & vbCr & _
objProcess.Name

' Check to see if vmware.exe is currently running
If InStr(objProcess.CommandLine, "vmware-vmx.exe") Then
' vmware is running if the above statement is true.
' Tell the user they must quit vmware.exe before the shutdown can
occur.
MsgBox("A virtual machine appears to be running. Please, shut it
down
before continuing.")
'Displays the process list output
WSCript.Echo strList
Else
'Create another variable to connect to the Windows shell service.
Dim objShell
' Connect to the Windows shell service
Set objShell = CreateObject("WScript.Shell")
' Execute the logoff command to logoff
objShell.Run "shutdown -l"
' Quit the script
WScript.Quit
End If
Next
WScript.Quit

Re: Logoff Script
Jeffery Hicks <jhicks[ at ]sapien.com> 6/27/2007 8:29:25 PM
camille.reeves[ at ]gmail.com wrote:
[Quoted Text]
> I'm trying to write a script that will check and see if a particular
> program is running on a computer (in my case VMware Workstation)
> whenever someone tries to logoff and prevent the logoff from
> happening
> if found. In the past people have logged off computers while VMware
> was running and usually these virtual machines would be running QE
> tests at the time so it would always be a real pain when they were
> shut off without warning. I want my script to check for one of the
> virtual machine's executable files (vmware-vmx.exe) and, if found,
> prompt a warning to the user that says something to the effect of
> "logoff canceled, VMs are currently running on this computer" then
> prevent the logoff process from happening. Otherwise, if the
> executible isn't found, let the user logoff.
>
> I've edited group policy so that logoff isn't displayed in the start
> menu or in Ctrl+Alt+Del which forces all users to run my script if
> they want to logoff. The only problem is my script doesn't work.
> When
> I click on it, it logs me off regardless of whether or not a virtual
> machine is running. I can't figure out why. I would appreciate some
> help and feedback.
>
>
> ' Require all variables to be declared before they can be used.
> ' This helps with debugging issues
> Option Explicit
>
> ' Define all of the variables
> Dim strComputer, objWMIService, colProcesses, objProcess, strList
>
> ' Seting strComputer to "." connects to the local machine.
> strComputer = "."
>
> ' Connect to the Windows Management Instruments interface
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" _
> & strComputer & "\root\cimv2")
> ' the impersonate line is for security clearance
> ' in order to query another machine's hardware
>
> ' Get a collection of all running processes
> Set colProcesses = objWMIService.ExecQuery("Select * FROM
> Win32_Process")
>
> ' Cycle through every currently running process
> For Each objProcess in colProcesses
> 'outputs process list
> strList = strList & vbCr & _
> objProcess.Name
>
> ' Check to see if vmware.exe is currently running
> If InStr(objProcess.CommandLine, "vmware-vmx.exe") Then
> ' vmware is running if the above statement is true.
> ' Tell the user they must quit vmware.exe before the shutdown can
> occur.
> MsgBox("A virtual machine appears to be running. Please, shut it
> down
> before continuing.")
> 'Displays the process list output
> WSCript.Echo strList
> Else
> 'Create another variable to connect to the Windows shell service.
> Dim objShell
> ' Connect to the Windows shell service
> Set objShell = CreateObject("WScript.Shell")
> ' Execute the logoff command to logoff
> objShell.Run "shutdown -l"
> ' Quit the script
> WScript.Quit
> End If
> Next
> WScript.Quit
>

Let's cover the basics first:
Can you verify that script works as expected if you run it manually?
Is the account you are using getting the GPO? If you are logged on as
domain admin, GPOs won't apply to you unless you specifically configure
them to do so.

If the computer is already shutting down, you shouldn't need to call the
shutdown command. You should be able to simply let it continue with the
process.

All that said, which is still good to check, the problem is that you
should use objProcess.caption and not objProcess.commandline. The
former is the full name you are looking for and the latter is just the
name without the extension.

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified.

blog: http://blog.SAPIEN.com
Community: http://www.scriptinganswers.com
Training: http://www.ScriptingTraining.com
Books: http://www.SAPIENPress.com
Editor: http://www.primalscript.com
Tools: http://www.scriptingoutpost.com

"Those who forget to script it are doomed to repeat it."

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen