|
|
I created a script that takes the place of the logoff on my computer. The logoff button in the start menu and Ctrl+Alt+Del menu has been disabled via group policy to prevent users from logging off while VMware is running. If users want to logoff, they have to double- click the script on the desktop. The script checks for the VMware executible file and if found, alerts the user and displays how many virtual machines are running. Otherwise, if VMware isn't found, the script will log off the user. The script works fine, the only problem is when the script displays a message box to alert the user about virtual machines that are running, it displays the same message for every VM that it finds. For example, instead of saying
4 VM(s) running. Please turn off the VMs before continuing.
It will say
1 VM(s) running. Please turn off the VMs before continuing. 2 VM(s) running. Please turn off the VMs before continuing. 3 VM(s) running. Please turn off the VMs before continuing. 4 VM(s) running. Please turn off the VMs before continuing.
How do I fix my script so that it only displays the last string/line "4 VMs running. Please turn off the VM before continuing."
' ************************************************************* ' LOGOFF SCRIPT ' Created: 6/14/2007 ' Updated: 7/3/2007 ' ' A big THANK YOU to Adam, Jaker, and Guy Thomas ' *************************************************************
' 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 Dim strList, VMcount
' Establish variables strComputer = "." ' Seting strComputer to "." connects to the local machine. VMcount = 0 ' starts the virtual machine count at 0
' All WMI scripts begin by connecting to the Windows Management Instruments interface Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2")
' 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 'Check to see if a virtual machine is currently running If objProcess.Name = "vmware-vmx.exe" Then ' vmware is running if the above statement is true. VMcount = VMcount + 1 'Count the number of VMs running 'Build the message box notifying the user of any VM processes strList = strList & VMcount _ & " VMware process(es) running." _ & " Please power off before continuing to logoff." & vbCr End If Next If VMcount > 0 Then ' This message box will only appear if the vmware-vmx process was found WSCript.Echo strList Else ' Create another variable to connect to the Windows shell service. Dim objWSHShell ' Connect to the Windows shell service Set objWSHShell = CreateObject("WScript.Shell") ' Execute the logoff command to logoff objWSHShell.Run "shutdown -l" End if ' Quit the script WScript.Quit
|
|
|
|
|