start/stop 服务时无法获得可靠的布尔值
Unable to get reliable boolean value when start/stop services
您好,我在尝试为我正在执行的部分脚本添加可靠性时遇到了一些问题。
我创建了一个 sub
来查询服务,它首先检查该服务是否 运行 然后停止该服务。我尝试了几种不同的方法来通过 WMI 检查服务是否为 运行,但是当我尝试使用 objSrvc.Started
生成布尔值时得到的结果不可靠,但这是证明不可靠,当它到达脚本中的点时,服务已成功停止,然后通过 Started
方法继续查询 return True
或 False
取决于首次使用 Started
方法时的服务状态。
Dim objReg : Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\"& strServer &"\root\default:StdRegProv")
Dim objFso : Set objFso = CreateObject("Scripting.FileSystemObject")
Dim objShll : Set objShll = CreateObject("Wscript.Shell")
Dim objSrvCnsl : Set objSrvCnsl = GetObject("winmgmts://./root/cimv2")
'File for output of data for testing purposes
'Set objOutFile = objFSO.CreateTextFile("C:\temp\Output.txt")
Function strSrcvName()
strRegPth = "SYSTEM\CurrentControlSet\Services"
objReg.EnumKey HKEY_LOCAL_MACHINE, strRegPth, arrReg
For Each strSubKey in arrReg
WScript.Interactive = False
Wscript.echo strSubKey
WScript.Interactive = True
strFullKey = strRegPth &"\"& strSubKey
objReg.GetStringValue HKEY_LOCAL_MACHINE, strFullKey , "DisplayName", strAppName
On Error Resume Next
'Test output of strAppName relates to DisplayName, REG_SZ value
'objOutFile.Write strAppName
'objOutFile.Write strSubKey
If strAppName = strSrvcIn Then Exit For
next
strSrcvName = strSubKey
End Function
'Wscript.Echo "Step 2"
Set objSrvc = objSrvCnsl.Get("Win32_Service.Name='"& strSrcvName & "'")
Sub objStpStrtSrvc()
If objSrvc.Started = True then
intSrvcStt = objSrvc.StopService()
If intSrvcStt = 0 then Wscript.Echo "Service has been stopped"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End if
Wscript.sleep(500)
If objSrvc.Started = False then
intSrvcStt = objSrvc.StartService()
If intSrvcStt = 0 then Wscript.Echo "Service has been started"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End If
End Sub
有人可以向我解释为什么我的变量在服务状态更改时没有更新其值吗?
如果这还不够清楚,我深表歉意。我找到了另一种方法,但它不是很容错(说实话,这也不是我想做的,但现在还很早,还有很多东西要添加),我停止了服务等待 500 毫秒,检查后如果服务处于 stopping
状态,如果不是,则它会调用新的 sub
来启动服务。
Sub objStpStrtSrvc()
If objSrvc.Started = True then
intSrvcStt = objSrvc.StopService()
If intSrvcStt = 0 then Wscript.Echo "Service has been stopped"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End if
If objSrvc.Status = "Stopping" then
Wscript.Sleep(500)
If objSrvc.Status = "Stopping" then
Wscript.Sleep(500)
If objSrvc.Status = "Stopping" then
Wscript.Sleep(500)
If objSrvc.Status = "Stopping" then
Wscript.Sleep(5000)
objStart()
End If
Elseif objSrvc.Status = "OK" then
objStart()
End If
Elseif objSrvc.Status = "OK" then
objStart()
End If
Elseif objSrvc.Status = "OK" then
objStart()
End If
End Sub
Sub objStart()
intSrvcStt = objSrvc.StartService()
If intSrvcStt = 0 then Wscript.Echo "Service has been started"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End If
End Sub
已解决!
有人向我解释说我需要再次调用服务对象,以刷新它的属性。
Sub objStpStrtSrvc()
If objSrvc.Started then
objSrvc.StopService()
intSrvcStt = objSrvc.StopService()
Wscript.Echo "Stopping Service"
If intSrvcStt = 0 then Wscript.Echo "Service has been stopped"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End if
wscript.sleep 500
wscript.echo intSrvcStt
Set objSrvc = objSrvCnsl.Get("Win32_Service.Name='"& strDerp & "'")
If not objSrvc.Started then
wscript.echo "Got Here2"
'intSrvcStt = objSrvc.StartService()
objSrvc.StartService()
Wscript.Echo "Starting Service"
If intSrvcStt = 0 then Wscript.Echo "Service has been started"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End If
End Sub
您好,我在尝试为我正在执行的部分脚本添加可靠性时遇到了一些问题。
我创建了一个 sub
来查询服务,它首先检查该服务是否 运行 然后停止该服务。我尝试了几种不同的方法来通过 WMI 检查服务是否为 运行,但是当我尝试使用 objSrvc.Started
生成布尔值时得到的结果不可靠,但这是证明不可靠,当它到达脚本中的点时,服务已成功停止,然后通过 Started
方法继续查询 return True
或 False
取决于首次使用 Started
方法时的服务状态。
Dim objReg : Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\"& strServer &"\root\default:StdRegProv")
Dim objFso : Set objFso = CreateObject("Scripting.FileSystemObject")
Dim objShll : Set objShll = CreateObject("Wscript.Shell")
Dim objSrvCnsl : Set objSrvCnsl = GetObject("winmgmts://./root/cimv2")
'File for output of data for testing purposes
'Set objOutFile = objFSO.CreateTextFile("C:\temp\Output.txt")
Function strSrcvName()
strRegPth = "SYSTEM\CurrentControlSet\Services"
objReg.EnumKey HKEY_LOCAL_MACHINE, strRegPth, arrReg
For Each strSubKey in arrReg
WScript.Interactive = False
Wscript.echo strSubKey
WScript.Interactive = True
strFullKey = strRegPth &"\"& strSubKey
objReg.GetStringValue HKEY_LOCAL_MACHINE, strFullKey , "DisplayName", strAppName
On Error Resume Next
'Test output of strAppName relates to DisplayName, REG_SZ value
'objOutFile.Write strAppName
'objOutFile.Write strSubKey
If strAppName = strSrvcIn Then Exit For
next
strSrcvName = strSubKey
End Function
'Wscript.Echo "Step 2"
Set objSrvc = objSrvCnsl.Get("Win32_Service.Name='"& strSrcvName & "'")
Sub objStpStrtSrvc()
If objSrvc.Started = True then
intSrvcStt = objSrvc.StopService()
If intSrvcStt = 0 then Wscript.Echo "Service has been stopped"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End if
Wscript.sleep(500)
If objSrvc.Started = False then
intSrvcStt = objSrvc.StartService()
If intSrvcStt = 0 then Wscript.Echo "Service has been started"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End If
End Sub
有人可以向我解释为什么我的变量在服务状态更改时没有更新其值吗?
如果这还不够清楚,我深表歉意。我找到了另一种方法,但它不是很容错(说实话,这也不是我想做的,但现在还很早,还有很多东西要添加),我停止了服务等待 500 毫秒,检查后如果服务处于 stopping
状态,如果不是,则它会调用新的 sub
来启动服务。
Sub objStpStrtSrvc()
If objSrvc.Started = True then
intSrvcStt = objSrvc.StopService()
If intSrvcStt = 0 then Wscript.Echo "Service has been stopped"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End if
If objSrvc.Status = "Stopping" then
Wscript.Sleep(500)
If objSrvc.Status = "Stopping" then
Wscript.Sleep(500)
If objSrvc.Status = "Stopping" then
Wscript.Sleep(500)
If objSrvc.Status = "Stopping" then
Wscript.Sleep(5000)
objStart()
End If
Elseif objSrvc.Status = "OK" then
objStart()
End If
Elseif objSrvc.Status = "OK" then
objStart()
End If
Elseif objSrvc.Status = "OK" then
objStart()
End If
End Sub
Sub objStart()
intSrvcStt = objSrvc.StartService()
If intSrvcStt = 0 then Wscript.Echo "Service has been started"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End If
End Sub
已解决!
有人向我解释说我需要再次调用服务对象,以刷新它的属性。
Sub objStpStrtSrvc()
If objSrvc.Started then
objSrvc.StopService()
intSrvcStt = objSrvc.StopService()
Wscript.Echo "Stopping Service"
If intSrvcStt = 0 then Wscript.Echo "Service has been stopped"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End if
wscript.sleep 500
wscript.echo intSrvcStt
Set objSrvc = objSrvCnsl.Get("Win32_Service.Name='"& strDerp & "'")
If not objSrvc.Started then
wscript.echo "Got Here2"
'intSrvcStt = objSrvc.StartService()
objSrvc.StartService()
Wscript.Echo "Starting Service"
If intSrvcStt = 0 then Wscript.Echo "Service has been started"
Elseif intSrvcStt = 2 then Wscript.Echo "Insufficient privileges"
Elseif intSrvcStt > 2 then Wscript.Echo "There is an issue, investigate"
End If
End Sub