2017-04-06 73 views
0

我有波紋管腳本如何停止錯誤的電源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";在開始。

我也有一些泊塢窗params

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"線我都會有錯誤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.

在情況下,如果我把後搬運工PARAMS $ErrorActionPreference = "Stop";線,腳本繼續運行這不是我想要的。

我不知道我需要在這裏做,所以我會任何幫助

+0

的PARAMS不只是碼頭工人。他們是爲劇本。你有沒有嘗試把你的行放在括號內?例如我試圖做到這一點,但我不知道在這裏使用什麼語法 我試圖做'[String] $ ErrorActionPreference =「停止」在下面'[String] $ Environment =「Debug」' – gms0ulman

+0

是的, ' – nightmare

回答

2

$ErrorActionPreference不帶命令行實用工具,比如搬運工的工作,因爲他們沒有在PowerShell中拋出異常感激。您將不得不使用returncode/errorlevel或解析輸出來處理這些類型的錯誤。有用的自動變量:

$?
包含上次操作的執行狀態。它包含 如果上次操作成功則爲TRUE,如果失敗則包含FALSE。

$ LastExitCode

包含上次運行的基於Windows的程序的退出代碼。與cmd中的%errorlevel%相同。

如果您檢測到錯誤,您可以拋出異常來停止腳本或使用類似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 

如果您要創建一個先進的功能,你可以設置爲cmdlet的這樣範圍默認ErrorAction:

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