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
$ErrorActionPreferencevariable, and do not catchWrite-Errorin try/catch blocks.
When using pSake:
- The last two calls work perfectly
- Using the ISE or the
-Fileparameter 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.
Not an answer, just a note.
I searched for explanation of
-fileparameter. Most sources say only “Execute a script file.”. At http://technet.microsoft.com/en-us/library/dd315276.aspx I readAfter that I tried to call this:
Note that first two stop after
Get-Foo, but the last one doesn’t.The problem I describe above is related to modules — if you define
Get-Fooinside script.ps1, all the 3 calls I described stop after call toGet-Foo.Just try to define it inside the script.ps1 or dotsource the file with
Get-Fooand check it. There is a chance it will work 🙂