Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke an exe from PowerShell and get feedback on success or failure

Tags:

powershell

How can I run an executable in PowerShell and through an if statement determine whether it succeeded or failed?

More specifically I'm trying to get devenv.exe to build a solution from a PowerShell script and I need to know whether it succeeded or failed. By failed, I mean that the build has failed and I'm assuming devenv is sending something out to the shell (possibly in the stderr stream?)

I tried using &, Invoke-Expression and Invoke-Item and managed to get all of them to run the exe. But I was never able to get feedback on success / failures.

like image 937
urig Avatar asked Jan 17 '10 17:01

urig


People also ask

What is invoke-expression in PowerShell?

Description. The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression , a string submitted at the command line is returned (echoed) unchanged. Expressions are evaluated and run in the current scope.

What is PowerShell invoke WebRequest?

The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web page or web service. It parses the response and returns collections of links, images, and other significant HTML elements. This cmdlet was introduced in PowerShell 3.0.

What is Lastexitcode in PowerShell?

Use the command Exit $LASTEXITCODE at the end of the powershell script to return the error codes from the powershell script. $LASTEXITCODE holds the last error code in the powershell script. It is in form of boolean values, with 0 for success and 1 for failure.


1 Answers

Have you tried using the $LASTEXITCODE variable? It will contain the exit code of the last .exe that was invoked.

  • https://devblogs.microsoft.com/powershell/errorlevel-equivalent/
like image 176
JaredPar Avatar answered Sep 24 '22 05:09

JaredPar