After writing deployment scripts from within the ISE, we need our continuous integration (CI) server to be able to run them automatically, i.e. from the command line or via a batch file.
I have noticed some significant differences between the following calls:
powershell.exe -File Script.ps1
powershell.exe -Command "& '.\Script.ps1'"
powershell.exe .\Script.ps1
Some simple examples:
- When using
-File
, errors are handled in the exact same way as the ISE. - The other two calls seem to ignore the
$ErrorActionPreference
variable, and do not catchWrite-Error
in try/catch blocks.
When using pSake:
- The last two calls work perfectly
- Using the ISE or the
-File
parameter will fail with the following error:
The variable '$script:context' cannot be retrieved because it has not been set
What are the implications of each syntax, and why they are behaving differently? I would ideally like to find a syntax that works all the time and behaves like the ISE.
try { remove-item nonexisting -ea 0 } catch { 'err occured' }
and what if I change -ea to 2? IMHO write-error is not trapped in catch block. – Advancement