簡體   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