简体   繁体   中英

Unable to get reliable boolean value when start/stop services

Hi I'm having a few issues attempting to add reliability to part of a script I'm doing.

I've created a sub to query a service, it first checks if the service is running & then stops said service. I've tried a few different methods to check if the service is running via WMI, but I'm getting unreliable results when I've attempted to use objSrvc.Started to produce a boolean value, but this is proving unreliable, when it reaches the point in the script that the service has successfully been stopped and then querying via the Started method continues to return True or False depending on the state of the service when the Started method is first used.

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

Can someone explain to me why my variable is not having its value updated when the service state changes?

I apologise if this isn't clear enough. I have found an alternative method but it isn't very fault tolerant (tbh neither is what I'm wanting to do, but it's early days there is still a lot to be added), I stopped the service waited 500ms, which after checking if the service was in the stopping state, if it wasn't then it called upon the new sub to start the service.

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

Solved!

It's been explained to me that I need to call the service object again, to refresh it's attributes.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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