在ISE中编写部署脚本后,我们需要持续集成(CI)服务器能够自动运行它们,即通过命令行或批处理文件。
我注意到以下调用之间存在一些显著差异:
powershell.exe -File Script.ps1
powershell.exe -Command "& '.\Script.ps1'"
powershell.exe .\Script.ps1
一些简单的例子:
- 使用
-File
时,错误处理方式与ISE完全相同。 - 另外两个调用似乎忽略了
$ErrorActionPreference
变量,并且在try/catch块中没有捕获Write-Error
。
使用pSake时:
- 最后两个调用完美运行
- 使用ISE或
-File
参数将会失败,报以下错误:
The variable '$script:context' cannot be retrieved because it has not been set
每种语法的含义是什么,它们为什么会有不同的行为?我希望找到一种语法,它可以始终正常工作并且像ISE一样表现。
try { remove-item nonexisting -ea 0 } catch { 'err occured' }
如果我将-ea改为2,会发生什么?在我的看法中,write-error并没有被捕获在catch块中。 - stej