2017-01-09 1 views
1

일부 cmdlet에 대해 try/catch 메서드를 사용하면 이상한 문제가 발생합니다.Powershell에서 오류 잡기

조언을 해주실 수 있습니까?

try 
{ 
$LookingForRemoteMailboxOnPrem = Get-RemoteMailbox $info -ErrorAction Stop | select -ExpandProperty UserPrincipalName 
} 
catch 
{ 
string]$t = $Error[0] 
} 

을하지만이 하나가되지 않습니다 : :

이 하나가 제대로 작동

try 
{ 
$EnableRemoteMailbox = Enable-RemoteMailbox $info -RemoteRoutingAddress $remote -PrimarySmtpAddress $info2 -ErrorAction Stop 
} 
catch 
{ 
[string]$t = $Error[0] 
} 

저장하지 오류를 $에 변수

+2

처리되지 않는 오류는 무엇입니까? '$ ErrorActionPreference = 'Stop''을 설정하면 똑같은 행동을합니까? –

+0

대신'$ t = $ _'을 시도하십시오. –

+0

@AnsgarWiechers $ ErrorActionPreference가 계속 설정되었습니다. – user3574248

답변

0

$ErrorActionPreference (T)는 기본적으로 Continue로 설정됩니다. 즉, PowerShell이 ​​오류에서 "복구"할 수 있으면 예외를 throw하지 않습니다. -ErrorAction 매개 변수를 사용하여 모든 cmdlet의 동작을 변경할 수 있습니다.

link 좋은 예를 제공합니다

Try {dir c:\missingFolder} Catch [System.Exception] {"Caught the exception"} Finally {$error.Clear() ; "errors cleared"}

문자열 "Caught the exception PowerShell을 창에 발생하지 않습니다. -ErrorActionStop으로 설정하면 예외가 발생합니다.

세부 사항은 here입니다.