[英]PowerShell 2.0 - Running scripts for the command line call vs. from the ISE
After writing deployment scripts from within the ISE, we need our continuous integration (CI) server to be able to run them automatically, ie from the command line or via a batch file. 从ISE中编写部署脚本后,我们需要我们的持续集成 (CI)服务器能够自动运行它们,即从命令行或通过批处理文件运行它们。
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: 一些简单的例子:
-File
, errors are handled in the exact same way as the ISE . -File
,错误的处理方式与ISE完全相同。 $ErrorActionPreference
variable, and do not catch Write-Error
in try/catch blocks. $ErrorActionPreference
变量,并且没有在try / catch块中捕获Write-Error
。 -File
parameter will fail with the following error: -File
参数将失败,并显示以下错误: 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.
理想情况下,我想找到一种语法,该语法一直有效并且表现得像ISE。
Not an answer, just a note. 没有答案,只有一个音符。
I searched for explanation of -file
parameter. 我搜索了
-file
参数的解释。 Most sources say only "Execute a script file.". 大多数资料只说“执行脚本文件”。 At http://technet.microsoft.com/en-us/library/dd315276.aspx I read
在http://technet.microsoft.com/zh-cn/library/dd315276.aspx上,我阅读了
Runs the specified script in the local scope ("dot-sourced"), so that the functions
and variables that the script creates are available in the current session. Enter
the script file path and any parameters.
After that I tried to call this: 之后,我试图称呼它为:
powershell -command ". c:\temp\aa\script.ps1"
powershell -file c:\temp\aa\script.ps1
powershell -command "& c:\temp\aa\script.ps1"
Note that first two stop after Get-Foo
, but the last one doesn't. 请注意,前两个在
Get-Foo
之后停止,但最后一个没有。
The problem I describe above is related to modules -- if you define Get-Foo
inside script.ps1, all the 3 calls I described stop after call to Get-Foo
. 我上面描述的问题与模块有关-如果在script.ps1中定义
Get-Foo
,则我描述的所有3个调用都在调用Get-Foo
之后停止。
Just try to define it inside the script.ps1 or dotsource the file with Get-Foo
and check it. 只需尝试在script.ps1内定义它,或使用
Get-Foo
对文件进行Get-Foo
并进行检查。 There is a chance it will work :) 有机会起作用:)
Here is a concrete example of the behaviour I described. 这是我描述的行为的具体示例。
MyModule.psm1 MyModule.psm1
function Get-Foo
{
Write-Error 'Failed'
}
Script.ps1 Script.ps1
$ErrorActionPreference = 'Stop'
$currentFolder = (Split-Path $MyInvocation.MyCommand.Path)
Import-Module $currentFolder\MyModule.psm1
try
{
Get-Foo
Write-Host "Success"
}
catch
{
"Error occurred"
}
Running Script.ps1: 运行Script.ps1:
From the ISE, or with the -File
parameter 从ISE或使用
-File
参数
will output "Error occurred" and stop
将输出“发生错误”并停止
From the command line without the -File
parameter 从命令行不带
-File
参数
will output "Failed" followed by "Success" (ie not caught)
将输出“失败”,然后输出“成功”(即未捕获)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.