Jump to content

Unchanged service status issue


 Share

Recommended Posts

DPS Service can't get status to "Stopped" return value is always 1

#RequireAdmin

If @OSVersion = "WIN_10" Then
    $sStatus = _CheckService("DPS")
    If $sStatus = 1 Then
        MsgBox(0, "", "Status1 : " & $sStatus)
        RunWait(@ComSpec & " /c " & 'net stop DPS', "", @SW_HIDE)
        MsgBox(0, "", "Status2 : " & $sStatus)
        RunWait(@ComSpec & " /c " & 'net start DPS', "", @SW_HIDE)
    Else
        RunWait(@ComSpec & " /c " & 'net start DPS', "", @SW_HIDE)
    EndIf
EndIf

Func _CheckService($s_ServiceName)
    Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    Local $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_Service WHERE Name = "' & $s_ServiceName & '"')
    If Not IsObj($colItems) Then Exit MsgBox(0, "", "Not an object : DPS", 5)
    If Not $colItems.count Then Return 0 ; Service not found
    For $oItem In $colItems
        If $oItem.State = "Running" Then Return 1
        If $oItem.State = "Stopped" Then Return 2
        If $oItem.State = "Not Installed" Then Return 3
    Next
EndFunc   ;==>_CheckService

 

Link to comment
Share on other sites

I couldn't answer because of my busy work, now I tried again with different scenarios, my problem still continues.

Stops the service after Script Ends.

#RequireAdmin
$oShell = ObjCreate("shell.application")

If @OSVersion = "WIN_10" Then
    $sStatus = _CheckService("DPS")
    If $sStatus = 1 Then
        $oShell.ServiceStop("DPS", False)
        MsgBox(0, "$oShell.ServiceStop", $sStatus)
        ;or
        _StopService("DPS")
        MsgBox(0, "_StopService", $sStatus)
        ;or
        RunWait(@ComSpec & " /c " & 'net stop DPS', "", @SW_HIDE)
        MsgBox(0, "RunWait net stop", $sStatus)
    Else
        MsgBox(0, "", "Status 2 And 3" & $sStatus)
        RunWait(@ComSpec & " /c " & 'net start DPS', "", @SW_HIDE)
    EndIf
EndIf

Func _CheckService($CheckServiceServiceName)
    Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    Local $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_Service WHERE Name = "' & $CheckServiceServiceName & '"')
    If Not IsObj($colItems) Then Exit MsgBox(0, "", "Not an object : DPS", 5)
    If Not $colItems.count Then Return 0 ; Service not found
    For $oItem In $colItems
        If $oItem.State = "Running" Then Return 1
        If $oItem.State = "Stopped" Then Return 2
        If $oItem.State = "Not Installed" Then Return 3
    Next
EndFunc

Func _StopService($StopServiceName)
    Local $oWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Local $oServiceList = $oWMI.ExecQuery('SELECT * FROM Win32_Service WHERE Name = "' & $StopServiceName & '"')
    For $sService In $oServiceList
        $sService.StopService()
    Next
EndFunc

 

Link to comment
Share on other sites

@youtuberWhat part of the following did you not understand? 🤨

Quote

You aren't retrieving the updated status following the "net stop DPS" command.

FWIW, this simplified example works as I would expect --

#RequireAdmin
$sStatus = _CheckService("DPS")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sStatus = ' & $sStatus & @CRLF & '>Error code: ' & @error & @CRLF)

RunWait(@ComSpec & " /c " & 'net stop DPS')

; Recheck status <== This is the step you are missing
$sStatus = _CheckService("DPS")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sStatus = ' & $sStatus & @CRLF & '>Error code: ' & @error & @CRLF)

Func _CheckService($CheckServiceServiceName)
    Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    Local $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_Service WHERE Name = "' & $CheckServiceServiceName & '"')
    If Not IsObj($colItems) Then Exit MsgBox(0, "", "Not an object : DPS", 5)
    If Not $colItems.count Then Return 0 ; Service not found
    For $oItem In $colItems
        If $oItem.State = "Running" Then Return 1
        If $oItem.State = "Stopped" Then Return 2
        If $oItem.State = "Not Installed" Then Return 3
        If $oItem.State = "Stop Pending" Then Return 4
    Next
EndFunc

 

Link to comment
Share on other sites

@Danp2 Thanks but... 

Earlier I have seen a code snippet that checks service status without recursively checking or checking from the loop.
But I don't remember under which topic I found those codes.
I guess it was something like this.

Local Static $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE name = "' & $CheckServiceServiceName & '" and state = 'Running'")

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...