2010-02-02 2 views
125

이것은 PowerShell 2.0의 try catchPowerShell 2.0 예외에 액세스하는 방법을 잡으려고

$urls = "http://www.google.com", "http://none.greenjump.nl", "http://www.nu.nl" 
$wc = New-Object System.Net.WebClient 

foreach($url in $urls) 
{ 
    try 
    { 
     $url 
     $result=$wc.DownloadString($url) 
    } 
    catch [System.Net.WebException] 
    { 
     [void]$fails.Add("url webfailed $url") 
    } 
} 

하지만 원하는 것은 C#

catch(WebException ex) 
{ 
    Log(ex.ToString()); 
} 

과 같은 것입니다.

답변

178

시도 뭔가 :

try { 
    $w = New-Object net.WebClient 
    $d = $w.downloadString('http://foo') 
} 
catch [Net.WebException] { 
    Write-Host $_.Exception.ToString() 
} 

예외는 $_ 변수입니다. 이 같은 $_을 탐험 있습니다

try { 
    $w = New-Object net.WebClient 
    $d = $w.downloadString('http://foo') 
} 
catch [Net.WebException] { 
    $_ | fl * -Force 
} 

내가 당신에게 당신이 필요로하는 모든 정보를 줄 것이라 생각합니다.

내 규칙 : 표시되지 않는 데이터가있는 경우 -force을 사용해보십시오.