Start-Process Error Handling

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of Kawika Moss Kawika Moss 1 year, 9 months ago.

  • Author
    Posts
  • #22867
    Profile photo of Kawika Moss
    Kawika Moss
    Participant

    I'm fairly new to PowerShell. I have a script that I am updating, that runs an application, passes some application switches, etc.. However, what this script does not account for is, if that console application does not start, because the service is down, or you can't connect to it, because the license is expired.

    So what I'd like to do is add some error handling for it, I tried adding a TRY CATCH but the script just keeps going, because the Start-Process doesn't throw an error that stops the script, so I saw that I cna put -ErrorAction stop and that should do it, however that didn't work either.

    What is the best way to account for such an issue so i can stop the script at the error and in the catch throw an error about it, and in the FINALLY section redirect the application to be started on one of the back up servers.

    Thanks,

    Kawika

  • #22871
    Profile photo of Kiran Reddy
    Kiran Reddy
    Participant

    Without seeing some code it is difficult to know what exactly is going on but i would think along these lines.

    1. Identify the condition for which the application\script should stop executing (a set amount of time perhaps)
    2. if condition above is met end the process using stop-process -processname
    3. Write your own custom error message in the catch block.

  • #22876
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Start-Process won't, and can't, throw an error, because as far as it's concerned the task completed without error – that is, the process started.

    If the process returns a nonzero exit code due to the conditions you describe, then you could capture that and check it.

    Otherwise, you'd need to proactively check the service, verify the license (not sure how), and so on before starting the process.

  • #22880
    Profile photo of Kawika Moss
    Kawika Moss
    Participant

    Yea, so I am currently doing a Get-Service and selecting the status and storing that in a variable, then using and IF statement to move through and assign accordingly, which is workign great, but unfortunately for this application, if the license is expired, the service could still be up, and the application won't let you connect. So to truly capture it, I need to start the application to verfiy I can connect, if not, throw an error.If Start-Process can't do that, is there a better way for me to try and connect to a console application to test if it will error or not, using PowerShell?

  • #22897
    Profile photo of Kawika Moss
    Kawika Moss
    Participant

    If I were to go about this a dfferent way, like use PowerShell to write a cmd prompt script, that returns an error code adn then store that error code as a variable in mypowershell script, would that be possible??

  • #22899
    Profile photo of Kawika Moss
    Kawika Moss
    Participant

    At this point, I have a .bat file that echos success or not if the license is good. So how can I execute this .bat file from powershell and capture what is echo'd??

    IF %ERRORLEVEL% EQU 0 (Echo No error found) ELSE (echo No License)

  • #22947
    Profile photo of Daniël
    Daniël
    Participant

    Use the following:

    $Output = Start-Process -FilePath c:\windows\system32\cmd.exe -ArgumentList '/c C:\YourCommand.bat' -Wait -NoNewWindow
    If($Output.Exitcode -ne 0)
    {
         Throw "Errorlevel $($Output.ExitCode)"
    }
    
  • #22951
    Profile photo of Kawika Moss
    Kawika Moss
    Participant

    Interesting, that is basically how I approacked it, but I did it as a function because I was gettign weird results if I put that code in my original script, now the function is returning the entire command and the results of the function...I'm not understanding that

You must be logged in to reply to this topic.