如何在出错时停止power shell脚本?

时间:2017-04-06 15:53:22

标签: bash powershell docker jenkins asp.net-core

我有下面的脚本

Dir C:\arts

它停在$ErrorActionPreference = "Stop";行,这对我有好处。正如我所理解的那样,我开始时会遇到Param( [Parameter(Mandatory=$True,ParameterSetName="Compose")] [switch]$Compose, [Parameter(Mandatory=$True,ParameterSetName="ComposeForDebug")] [switch]$ComposeForDebug, [Parameter(Mandatory=$True,ParameterSetName="StartDebugging")] [switch]$StartDebugging, [Parameter(Mandatory=$True,ParameterSetName="Build")] [switch]$Build, [Parameter(Mandatory=$True,ParameterSetName="Clean")] [switch]$Clean, [parameter(ParameterSetName="Compose")] [Parameter(ParameterSetName="ComposeForDebug")] [parameter(ParameterSetName="Build")] [parameter(ParameterSetName="Clean")] [ValidateNotNullOrEmpty()] [String]$Environment = "Debug" ) 行。

我也有一些码头params

$ErrorActionPreference = "Stop"

如果我在docker params之前添加Cannot convert value "System.String" to type "System.Management.Automation.SwitchParameter". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.行,我会收到错误$ErrorActionPreference = "Stop";

如果我在docker params之后放置{{1}}行,脚本将继续运行,这不是我想要的。

我不知道我在这里需要做什么,所以我将不胜感激任何帮助

1 个答案:

答案 0 :(得分:2)

$ErrorActionPreference doesn't work with command line utilities like docker as they don't throw exceptions in PowerShell. You would have to use returncode/errorlevel or parse the output to handle those type of errors. Useful automatic variables:

$?
Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

$LastExitCode

Contains the exit code of the last Windows-based program that was run. Same as %errorlevel% in cmd.

If you detect an error, you can throw an exception to stop the script or use something like exit to stop the script. Example:

function Test-Error {
    $ErrorActionPreference = "Stop"

    Write-Host Before
    ping -n 1 123.123.123.123

    #If last command was not successfull.
    #You can also have checked $lastexitcode, output etc.
    if($? -eq $false) {
        #Throw terminating error
        #throw "Error"

        #Or since we've chosen to stop on non-terminating errors, we could use:
        Write-Error -ErrorId $LASTEXITCODE -Message "Ping failed"
    }
    Write-Host After
}

Test-Error

Output:

Before

Pinging 123.123.123.123 with 32 bytes of data:
Request timed out.

Ping statistics for 123.123.123.123:
    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
Test-Error : Ping failed
At line:22 char:1
+ Test-Error
+ ~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : 1,Test-Error

If you're creating a advanced function, you could set the default ErrorAction for the scope of the cmdlet like this:

function Test-Error {
    [CmdLetBinding()]
    param(
        $Name = "World"
    )

    #If -ErrorAction is not specified by the user, use Stop for the scope of the function
    if(-not $MyInvocation.BoundParameters.ContainsKey("ErrorAction")) { $ErrorActionPreference = "Stop" }

    "Hello $Name ! My ErrorAction is: $ErrorActionPreference"
}

PS > $ErrorActionPreference
Continue

PS > Test-Error -ErrorAction Ignore
Hello World ! My ErrorAction is: Ignore

PS > Test-Error
Hello World ! My ErrorAction is: Stop