开发者

VBScript to shutdown Windows when a process ends?

I have a program that scans through data at the 开发者_StackOverflow中文版end of the night on some occasions. On those occasions, I would like to run a VBScript that will watch for that program to close, and when it does, will shut down Windows.

I created a .BAT file that runs the program and then shuts Windows down, but I don't always need to shutdown when I finish using the program.

So I would like to use the scanning program, and if, at the end of the night, I am ready to leave, but the program is still scanning, I would to open the VBScript that will watch for my scanning program to close.

Is this possible?

Windows 7 Ultimate
x64 UAC = ON


Well, I figured out how to do this via this post at Techimo.com.

Dim isRunning, wasRunningAtStart, strComputer, strShutdown, objWMIService 
Dim objcolProcesses, objShell, strProcesses, strProcessName

'boolean condition for the loop
isRunning = True
wasRunningAtStart = True

'-----Specify the computer name on which to watch a process:
strComputer = "." '>>> "." for this computer

'-----Specify the process to watch.  Must be enclosed in Single Quotes:
strProcessName = "'processname.exe'" '>>> Example: "'notepad.exe'"

Set objWMIService = GetObject("winmgmts:" & _
    "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
strProcesses = "SELECT * FROM Win32_Process WHERE Name = "
strShutdown = "shutdown -s -t 0 -f -m \\" & strComputer
Set objShell = CreateObject("WScript.Shell")

'Check the process once, no need to run if the process
'isn't already running
'Query WMI for the running processes matching our process name
Set objColProcesses = objWMIService.ExecQuery ( _
    strProcesses & strProcessName)

'If the process is running, the count will be greater than 0,
'so we switch our boolean here to exit the loop.
If objcolProcesses.Count = 0 Then
    wasRunningAtStart = False
    isRunning = False
End If 
Set objColProcesses = Nothing   

Do While isRunning
    'Wait 2 seconds, prevents this script from using the CPU
    WScript.Sleep 2000

    'Query WMI for the running processes matching our process name
    Set objColProcesses = objWMIService.ExecQuery ( _
        strProcesses & strProcessName)

    'If the process is running, the count will be greater than 0,
    'so we switch our boolean here to exit the loop.
    If objColProcesses.Count = 0 Then
        isRunning = False
    End If
Loop

If wasRunningAtStart Then
    'MsgBox "Would shutdown here"
    objShell.Run strShutdown
Else
    MsgBox "The specified program is not already running."
End If

Set objColProcesses = Nothing
Set objShell = Nothing
Set objWMIService = Nothing


' Shutdown.vbs
' Example VBScript to Shutdown computers
' Author Josh Murray
' Version 4.1 - February 2007
' --------------------------------------Option Explicit 
Dim objShell, strComputer, strInput 
Dim strShutdown

Do 
strComputer = (InputBox(" ComputerName to shutdown", "Computer Name"))
If strComputer <> "" Then 
  strInput = True 
End if 
Loop until strInput = True

    strShutdown = "shutdown -s -t 0 -f -m \\" & strComputer

    set objShell = CreateObject("WScript.Shell")

    objShell.Run strShutdown

Wscript.Quit
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜