2011-01-18 13 views
1

У меня есть программа, которая просматривает данные в конце ночи в некоторых случаях. В тех случаях я хотел бы запустить VBScript, который будет следить за закрытием этой программы, и когда это произойдет, отключит Windows.VBScript для завершения работы Windows при завершении процесса?

Я создал файл .BAT, который запускает программу, а затем отключает Windows, но мне не всегда нужно выключать, когда я заканчиваю использовать программу.

Поэтому я хотел бы использовать программу сканирования, и если в конце ночи я готов уйти, но программа все еще сканирует, я бы открыл VBScript, который будет следить за моей программой сканирования закрывать.

Возможно ли это?

Windows 7 Ultimate 
x64 UAC = ON 

ответ

1

Ну, я понял, как сделать это с помощью this post на 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 
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

Смежные вопросы