VBScript 在进程结束时关闭 Windows?

发布于 2024-10-12 13:05:45 字数 284 浏览 3 评论 0原文

我有一个程序,有时会在深夜扫描数据。在这些情况下,我想运行一个 VBScript 来监视该程序关闭,并在关闭时关闭 Windows。

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

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

这可能吗?

Windows 7 Ultimate
x64 UAC = ON

I have a program that scans through data at the 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空名 2024-10-19 13:05:45

好吧,我通过 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

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
说谎友 2024-10-19 13:05:45
' 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
' 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文