Powershell 2.0 - Running scripts for the command line call vs. from the ISE

Posted by Gromix on Stack Overflow See other posts from Stack Overflow or by Gromix
Published on 2010-05-24T03:48:34Z Indexed on 2010/05/24 4:31 UTC
Read the original article Hit count: 264

Filed under:
|
|
|
|

Hi,

After writing deployment scripts from within the ISE, we need our CI server to be able to run them automatically, i.e. from the command line or via a batch file.

I have notice 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 catch Write-Error in try/catch blocks.

When using pSake:

  • The last 2 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


Could someone help me understand 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.

Thanks,

Romain

© Stack Overflow or respective owner

Related posts about command-line

Related posts about powershell