VBScript在进程结束时关闭Windows?

时间:2011-01-18 09:53:51

标签: windows-7 vbscript process

我有一个程序可以在某些情况下在夜晚结束时扫描数据。在那些情况下,我想运行一个VBScript来监视该程序关闭,当它关闭时,将关闭Windows。

我创建了一个运行程序然后关闭Windows的.BAT文件,但是当我使用该程序时,我并不总是需要关闭。

所以我想使用扫描程序,如果在晚上结束时我准备离开,但是程序仍在扫描,我会打开VBScript来监视我的扫描程序关闭。

这可能吗?

Windows 7 Ultimate
x64 UAC = ON

2 个答案:

答案 0 :(得分:1)

' 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

答案 1 :(得分:1)

好吧,我在Techimo.com上通过this post想出了如何做到这一点。

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