Start-Process Error Handling

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.



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.

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.

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?

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

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)

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)"

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

Looks good on surface, however i receive the following when i run your code (with my own processes being invoked):

Errorlevel At C:\Netezza.ps1:11 char:6 + Throw "Errorlevel $($Output.ExitCode)" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (Errorlevel :String) [], RuntimeException + FullyQualifiedErrorId : Errorlevel