繁体   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