簡體   English   中英

powershell 2.0嘗試捕獲如何訪問異常

[英]powershell 2.0 try catch how to access the exception

這是PowerShell 2.0中的try catch

$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());
}

這可能嗎?

嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM