我正在尝试制作一个 vb 脚本,如果它崩溃,它将重新启动另一个 vb 脚本。
我搜索了又搜索,但我得到的只是如何重新启动程序,并且由于 vb 脚本是后台进程,因此当您在 Win32_Process 中搜索时它不起作用。
这是我的代码
set Service = GetObject ("winmgmts:")
set Shell = WScript.CreateObject("WScript.Shell")
sEXEName = "Test_To_Block.vbs"
while true
bRunning = false
for each Process in Service.InstancesOf ("Win32_Process")
if Process.Name = sEXEName then
bRunning=true
msgbox("I am active")
End If
next
if bRunning=False then
msgbox("I am not active.")
Shell.Run sEXEName
end if
WScript.Sleep(100)
wend
问题是它从来没有看到文件正在运行,只是打开了数百个“Test_To_Stop.vbs”,这解决了我必须重新启动计算机的问题。
在我看来,应该改变的是代码正在寻找的地方。
for each Process in Service.InstancesOf ("Win32_Process")
您不需要查看“Win32_Process”,而是需要查看后台进程运行的位置。
我是编码新手,如果这是一个简单的问题,我很抱歉。
先感谢您。
Regards,
A Viper
下面的代码通过以下方式自行重新启动WshShell.Exec()
正在运行的脚本的方法和跟踪状态通过.Status
返回对象的属性:
If Not WScript.Arguments.Named.Exists("task") Then
Do
With CreateObject("WScript.Shell").Exec("""" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""")
Do While .Status = 0
WScript.Sleep 1
Loop
End With
Loop
End If
MsgBox "This script will be restarted immediately after termination"
另一种方法是使用.Run()
第三个参数设置为的方法True
等待启动的进程终止:
If Not WScript.Arguments.Named.Exists("task") Then
Do
CreateObject("WScript.Shell").Run """" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""", 1, True
Loop
End If
MsgBox "This script will be restarted immediately after termination"
或者更简单:
If Not WScript.Arguments.Named.Exists("task") Then
Do
CreateObject("WScript.Shell").Run """" & WScript.ScriptFullName & """ ""/task""", 1, True
Loop
End If
MsgBox "This script will be restarted immediately after termination"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)