I have a wrapper script to execute other scripts remotely (by copying them to the remote computer and running them via Invoke-Command), but I'm having an issue where it behaves differently when run locally than using Invoke-Command.
If I execute this script locally, reg.exe doesn't throw a terminating error, no matter what $ErrorActionPreference is set to.
remote.ps1
$err = 0
# This makes no difference on reg.exe when executing locally, but it does when using Invoke-Command
#$ErrorActionPreference = 'Stop'
try {
& reg load HKU\AlreadyLoaded C:\Users\user\ntuser.dat
"reg.exe exited with code $LastExitCode"
if ($LastExitCode) { throw 'could not load user hive' }
} catch {
"Error caught: $_"
$err = 1
}
"Remote Script exiting with code $err"
exit $err
Output:
ERROR: The process cannot access the file because it is being used by another process.
reg.exe exited with code 1
Error caught: could not load user hive
Remote Script exiting with code 1
wrapper.ps1
$err = 0
$ErrorActionPreference = 'Stop'
try {
Invoke-Command -ComputerName foo -ScriptBlock {
Set-ExecutionPolicy Bypass -Scope Process -Force
& C:\remote.ps1
if ($LastExitCode -gt 0) { throw "Remote script exited with non-zero code" }
}
} catch {
Write-Warning $_
$err = 1
}
"Local Script exiting with code $err"
exit $err
Output without $ErrorActionPreference = 'Stop' in remote script:
(the exception thrown by reg.exe is caught by wrapper script)
WARNING: ERROR: The process cannot access the file because it is being used by another process.
Local Script exiting with code 1
Output with $ErrorActionPreference = 'Stop' in remote script: (the exception thrown by reg.exe is caught by remote script)
Error caught: ERROR: The process cannot access the file because it is being used by another process.
Remote Script exiting with code 1
WARNING: Remote script exited with non-zero code
Local Script exiting with code 1
I thought of using Invoke-Command with ErrorAction = 'Continue' but then how do I catch errors from the command itself, such as the remote computer being down?
What's the best approach to run a script remotely and have it behave like it was run locally?