2012-05-11 2 views
2

을 오도하는 것입니다.powershell 예외는 스크립트가 주어진 InvocationInfo

$foo = @("bar") 

try { 
    $foo | ForEach-Object { 
     Join-Path $null $null 
    } 
} catch { 
    $_.InvocationInfo.Line 
} 

$foo | ForEach-Object { 

를 인쇄하지만 나는

Join-Path $null $null 

가 어떻게 예외가 실제로 발생 된 곳 얻을 수시겠습니까?

답변

4

이렇게하면 실제 라인을 얻을 것이다 :

$_.Exception.CommandInvocation.Line 

및 예외 메시지 :

$_.Exception.Message 

과 줄 번호 :

$_.Exception.Line 

오프셋 (열) :

$_.Exception.Offset 

유용한 작은 메시지를 작성할 수 있습니다.

} catch { 
    $msg = "Failed to do something. Failed on line number '{0}' column '{1}' ('{2}'). The error was '{3}'." -f 
     $_.Exception.Line, $_.Exception.Offset, $_.Exception.CommandInvocation.Line.Trim(), $_.Exception.Message 
    Write-Error $msg 
} 
관련 문제