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

后端 未结 2 1411
死守一世寂寞
死守一世寂寞 2021-02-06 16:38

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 ba

2条回答
  •  我寻月下人不归
    2021-02-06 17:28

    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)

提交回复
热议问题