PowerShell 2.0 - 运行命令行调用与来自ISE的脚本 [英] PowerShell 2.0 - Running scripts for the command line call vs. from the ISE

查看:283
本文介绍了PowerShell 2.0 - 运行命令行调用与来自ISE的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ISE中编写部署脚本后,我们需要持续集成(CI)



我注意到以下调用之间的一些显着差异:

  powershell.exe -File Script.ps1 
powershell.exe -Command&'.\Script.ps1'
powershell.exe .\Script.ps1






一些简单的示例:




  • 使用 -File 方法与 ISE 完全相同。

  • 其他两个调用似乎忽略 $ ErrorActionPreference 变量,不捕获 Write-Error
  • /github.com/JamesKovacs/psakerel =nofollow noreferrer> pSake :




    • 最后两个通话工作完美

    • 使用ISE或 -File 参数将失败,并显示以下错误:



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






    每个语法的含义是什么,以及为什么它们的行为不同?我理想地希望找到一个能够一直工作的语法,就像ISE。

    解决方案

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



    我搜索了 -file 参数的解释。大多数来源只说执行脚本文件。在 http://technet.microsoft.com/en-us/library/dd315276 .aspx 我阅读

     在本地作用域(dot-sourced)中运行指定的脚本,函数
    和脚本创建的变量在当前会话中可用。输入
    脚本文件路径和任何参数。

    之后,我尝试调用:

      powershell -commandc:\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个调用在调用



    只需尝试在script.ps1中定义它或使用 Get-Foo 并检查它。有一个机会,它将工作:)


    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.

    解决方案

    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 :)

    这篇关于PowerShell 2.0 - 运行命令行调用与来自ISE的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆