1

Unfortunately, my host application can't directly execute PowerShell scripts, so I have written a batch script which calls PowerShell script with content

@echo off
echo calling upgrade product with argument %2
if [%1] == [] (
    powershell -ExecutionPolicy UnRestricted -command "%~dp0Product1.ps1 "ProductConfig.xml" -verbose; exit $LASTEXITCODE"
) else (
cd %1
powershell -ExecutionPolicy UnRestricted -command "%1\UpgradeProduct.ps1 %2 -verbose; exit $LASTEXITCODE"
)

And in my powershell script i have code like

$ErrorActionPreference="Stop"
try{
    Copy-Item -Path $source -Destination $dest
}catch{
    Write-Warning "Some Error"
}

This executes fine when i execute the script from the PowerShell window(if $source is not found it will throw a terminating error and prints Some Error). But when executed from batch script if $source is not found Copy-Item throws a non terminating error and continues(Dosen't print Some Error).
How can i make the Copy-Item to throw a terminating error if $Source is not found?

1
  • try to use return instead of exit Commented Jan 29, 2018 at 12:28

1 Answer 1

1

You have nothing that stop in your catch block. I presume Your Write-Warning is triggered, but code after your block is runned.

You must return something in your catch block, like:

$ErrorActionPreference="Stop"
try{
    Copy-Item -Path $source -Destination $dest
}catch{
    Write-Warning "Some Error"

    #$LASTEXITCODE is a special variable in powershell
    $LASTEXITCODE = 1
    exit $LASTEXITCODE
}

Note $LASTEXITCODE variable, it is a special variable, the equivalent to %errorlevel%, that is used by commands like "cmd calls" by PowerShell.

You can test it by using this command in PowerShell:

cmd.exe /c exit 5
$LASTEXITCODE

I suggest you to first do a check if path exists:

try{
    if(Test-Path $source){
        Copy-Item -Path $source -Destination $dest -ErrorAction Stop
    }else{
        Write-Warning "$source not found."
            $LASTEXITCODE = 1
                exit $LASTEXITCODE
    }
}catch{
    Write-Error "Exception during copy: $($_)"
        $LASTEXITCODE = 1
        exit $LASTEXITCODE
}

Point of interest: ErrorAction is used at Function level. Set it at script level will include this settings for many commands. Exception will be thrown only if copy fail for something else: bad ACL, overwrite etc.

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

4 Comments

The Write-Warning itself not triggered. It's just prints the error message and continues. It' won't print Some Error
Thanks for the update. I have multiple instructions in the try catch block and I don't want to modify all of them and test again. I have even created a PS profile which defaults $ErrorActionPreference="Stop". I think I will modify all instructions to handle errors separately.
$ErrorActionPreference="Stop" is not setted in your script? If you load it inside a profile, the problem should be that your profile is not loaded by batch. Try print is value of $ErrorActionPreference before your commands .
When I print $ErrorActionPreference before the cmdlet it shows it's set to Stop but still it throws a nonterminating error. When I pass -ErrorAction Stop argument then it behaves as expected. If I just execute the PowerShell script then it works as expected but when I call the script from a batch file it's not throwing terminating errors.

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.