PowerShell 2.0 - 为命令行调用运行脚本与从 ISE 运行脚本

发布于 2024-09-03 08:42:06 字数 930 浏览 8 评论 0原文

从 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 参数将失败并出现以下错误:

无法检索变量 '$script:context',因为尚未设置


每种语法的含义是什么,以及为什么它们的行为不同?理想情况下,我希望找到一种始终有效且行为类似于 ISE 的语法。

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 catch Write-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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮华 2024-09-10 08:42:06

不是答案,只是一个注释。

我搜索了 -file 参数的解释。大多数消息来源只说“执行脚本文件。”。在 http://technet.microsoft.com/en-us/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.

之后,我尝试调用此:

powershell -command ". c:\temp\aa\script.ps1"
powershell -file c:\temp\aa\script.ps1
powershell -command "& c:\temp\aa\script.ps1"

请注意,前两个在 Get-Foo 之后停止,但最后一个没有。

我上面描述的问题与模块有关 - 如果您在 script.ps1 中定义 Get-Foo,我描述的所有 3 个调用都会在调用 Get-Foo 后停止。

只需尝试在 script.ps1 中定义它或使用 Get-Foo 点源文件并检查它。有机会它会起作用:)

Not an answer, just a note.

I searched for explanation of -file parameter. Most sources say only "Execute a script file.". At http://technet.microsoft.com/en-us/library/dd315276.aspx I read

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.

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.

Just try to define it inside the script.ps1 or dotsource the file with Get-Foo and check it. There is a chance it will work :)

原谅我要高飞 2024-09-10 08:42:06

这是我所描述的行为的具体示例。

MyModule.psm1

function Get-Foo
{
    Write-Error 'Failed'
}

Script.ps1

$ErrorActionPreference = 'Stop'

$currentFolder = (Split-Path $MyInvocation.MyCommand.Path)
Import-Module $currentFolder\MyModule.psm1

try
{
    Get-Foo 
    Write-Host "Success"
}
catch
{
    "Error occurred"
} 

运行 Script.ps1:

  • 从 ISE,或使用 -File 参数

    <块引用>
    <块引用>

    将输出“发生错误”并停止

  • 从不带-File参数的命令行

    <块引用>
    <块引用>

    将输出“失败”,然后输出“成功”(即未捕获)

Here is a concrete example of the behaviour I described.

MyModule.psm1

function Get-Foo
{
    Write-Error 'Failed'
}

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:

  • From the ISE, or with the -File parameter

    will output "Error occurred" and stop

  • From the command line without the -File parameter

    will output "Failed" followed by "Success" (i.e. not caught)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文