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