Jump to content

getting a successful result from command prompt


Recommended Posts

So i tried setting sleep to (480000) and it still comes back with not running. Maybe the service I am using does shut down every now and again?

opt("WinWaitDelay",500)
Opt("WinTitleMatchMode",4)
Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",0)
sleep(10000)
$check = 1
#include <Constants.au3>
While $check = 1
$result= _CMDreturn('sc query "aawservice" | findstr "STATE"'); i have been using aawservice to test, actual service is different

;$string = StringRegExp($result, 'RUNNING', 1); look at string that is returned.

$loc = StringInStr($result, "RUNNING")         ;Where in the string is "RUNNING" located?

If $loc Then                                   ;$loc will be non-zero if the string was found
    $string = StringTrimRight(StringTrimLeft($result, $loc - 1), 3) 
Else
    MsgBox(0, "Error", "String 'RUNNING' not found.")
;run ('c:/scripts/restart.bat')
EndIf


Sleep(960000); this was 480000 I keep doubling to try to get this to work. 
WEnd

Func _CMDreturn($sCommand); This function returns the output of a DOS command as a string
    $cmdreturn = ""
    $stream = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDERR_MERGED + $STDIN_CHILD)
    While 1; loop through the return from the command until there is no more
        $line = StdoutRead($stream)
        If @error Then ExitLoop
        $cmdreturn &= $line
    WEnd
    Return $cmdreturn
EndFunc  ;==>_CMDreturn

Here is my original script with string reg exp with the same result.

While $check = 1
$result= _CMDreturn('sc query "aawservice"');call command prompt function
$string = StringRegExp($result, 'RUNNING', 1); look at string that is returned.
If @error == 0 Then
    $check = 1
Else
    $check = 2
EndIf
 if $check = 2 Then
  msgbox(0, "error", $result)
    ;run ('c:/scripts/restart.bat')
 EndIf 

 if $check = 2 Then 
     test($check)
     EndIf
 
 sleep(60000); wait 60 seconds and try again
 WEnd
Link to comment
Share on other sites

Ok it finally happened to me....it was sporadic at first, but it seems there's a pattern... Adding the following MsgBox to your error should help figure out what the problem is:

MsgBox(0, "Error", "String 'RUNNING' not found." & @CR & "$loc = " & $loc & @CR & "$string = " & $string & @CR & "$result = " & $result)

The MsgBox says $result is empty so maybe _CMDreturn isn't working properly after so many times? Have you considered having Windows Scheduled Tasks execute this script every so often?

Link to comment
Share on other sites

Ok it finally happened to me....it was sporadic at first, but it seems there's a pattern... Adding the following MsgBox to your error should help figure out what the problem is:

MsgBox(0, "Error", "String 'RUNNING' not found." & @CR & "$loc = " & $loc & @CR & "$string = " & $string & @CR & "$result = " & $result)

The MsgBox says $result is empty so maybe _CMDreturn isn't working properly after so many times? Have you considered having Windows Scheduled Tasks execute this script every so often?

Thanks for the code. Yah it doesn't happen every time just some of the time. I hadent thought of using a scheduled task, I might have to go that route. Its just funny that it does error out like that
Link to comment
Share on other sites

so i am kind of an idiot i think... I dont know why I didnt think of this before, but I just put an if statement that says keep checking until its not blank. Seems to be working, i will let it run for a little longer and let you know how it turns out.

if $result = "" Then
    Do 
    $result= _CMDreturn('sc query "aawservice" | findstr "STATE"')
    sleep(10000)
    until $result <> ""
EndIf
Link to comment
Share on other sites

Your script actually prompted me to develop some kind of Service Check function- never know when it could come in handy! I'll post if you want it. It will be something like ServiceCheck("servicename") and it will return whether or not the service even exists, if it's running, stopped, pending, ...and I'm still looking for what else it can return. The nice thing is that Windows returns an error # and Description so either can be used. So thank you for that! Now if I could just find the rest of the messages Windows will return for services...

_CMDReturn function will also be slightly modified to try to avoid the problem you were originally having, but the solution you came up with also seems to take care of it...credits to SpookMeister (I think?) for that function.

Link to comment
Share on other sites

  • 2 weeks later...

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...