2011-01-18 18 views
1

私はいくつかの機会に夜の終わりにデータをスキャンするプログラムを持っています。これらの機会に、私はVBScriptを実行して、そのプログラムが終了するのを確認し、終了するとWindowsをシャットダウンします。プロセス終了時にWindowsをシャットダウンするVBScript?

プログラムを実行してからWindowsをシャットダウンする.BATファイルを作成しましたが、プログラムの使用を終了すると必ずシャットダウンする必要はありません。

私はスキャンプログラムを使用したいと思います。夜の終わりに私は出発する準備ができていますが、プログラムはまだスキャンしています。スキャンプログラムを見るVBScriptを開きます閉じる。

これは可能ですか?

Windows 7 Ultimate 
x64 UAC = ON 

答えて

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 
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 
+0

これは実際に私が探しているものではありません。私はVBScriptを使ってコンピュータをシャットダウンする方法を知っていますが、それは私が手に入れているスクリプトの前に起動されたときに終了するプロセスを監視しています。 – Dexter

関連する問題