繁体   English   中英

启动/停止服务时无法获得可靠的布尔值

[英]Unable to get reliable boolean value when start/stop services

嗨,我在尝试为正在执行的脚本的一部分增加可靠性时遇到一些问题。

我创建了一个sub查询服务,它首先检查该服务是否正在运行,然后停止该服务。 我尝试了几种不同的方法来检查服务是否通过WMI运行,但是当我尝试使用objSrvc.Started得到了不可靠的结果。开始产生一个布尔值,但是事实证明,达到此值是不可靠的脚本中成功停止服务然后通过Started方法进行查询的点将继续返回TrueFalse具体取决于首次使用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

有人可以向我解释为什么服务状态更改时变量的值没有更新吗?

如果这还不够清楚,我深表歉意。 我找到了另一种方法,但是它不是很容错(tbh也不是我想要做的,但是现在还需要添加很多东西),我停止了服务,等待了500ms,在检查之后如果服务处于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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM