如何停止电源shell脚本的错误?

我有下面的脚本

$ErrorActionPreference = "Stop"; while($true) { try { Write-Host "Step 1"; Dir C:\arts #Error Write-Host "Step 2"; exit 0 break; } catch { "Error in " + $_.InvocationInfo.ScriptName + " at line: " + $_.InvocationInfo.ScriptLineNumber + ", offset: " + $_.InvocationInfo.OffsetInLine + "."; $Error exit 1 break; } } 

它停在方向Dir C:\arts线,这对我很好。 据我了解,发生因为我有行$ErrorActionPreference = "Stop"; 在开始。

我也有一些docker的参数

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

如果我把$ErrorActionPreference = "Stop"行之前,docker参数我将有错误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. 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参数之后,脚本继续运行,那不是我想要的。

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

$ErrorActionPreference不能用于像$ErrorActionPreference这样的命令行工具,因为它们不会在PowerShell中抛出exception。 您将不得不使用returncode / errorlevel或parsing输出来处理这些types的错误。 有用的自动variables:

$?
包含上次操作的执行状态。 如果最后一次操作成功则它包含TRUE,如果失败则包含FALSE。

$ LastExitCode

包含运行的最后一个基于Windows的程序的退出代码。 与cmd中的%errorlevel%相同。

如果检测到错误,则可以抛出exception来停止脚本或使用类似exit来停止脚本。 例:

 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 

输出:

 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 

如果你正在创build一个高级函数,你可以像下面这样设置默认的ErrorAction作为cmdlet的作用域:

 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