0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.