SoftwareSavant
SoftwareSavant

Reputation: 9747

Execute Powershell Script from powershell indicate it executed successfully

I am attempting to execute a powershell script from another powershell script. I want some way to indicate that the script executed successfully, or that It failed. And if it failed, I would like to pass the exception along.

Here is my basic sript...

$Scripts = gci -Path $BuildForgeDir -filter "*.ps1"

As you can see, I find all powershell scripts...

ForEach($File in $Scripts){
    Write-host("File: $File")
    & "$BuildForgeDir\$File" -projName $projName  -baseBfDir $baseDirA -debugOrRelease $debugOrRelease
}

And then I execute them...

How would I bubble up exceptions, failurs ect...

Upvotes: 0

Views: 408

Answers (2)

zerocool18
zerocool18

Reputation: 523

You can call another script using powershell.exe and then check that $LastExitCode is 0(0 means success). This is how Power Shell checks status of external applications.

Example :

powershell.exe -noprofile -file E:\test.ps1
If($LastExitCode -ne 0) {Throw "Error!"}

Upvotes: 0

Vikas Gupta
Vikas Gupta

Reputation: 4465

Exceptions bubble up automatically... If you still have concern, there must be something else, please clarify in the question..

As for the success / failure of the scripts. the scripts can also be thought of as a function in a file.. i.e. they can have a param block to take parameters, and return data. In your case. you can have it return a flag indicating success or failure.

Upvotes: 2

Related Questions