【问题标题】:powershell 2.0 try catch how to access the exceptionpowershell 2.0 try catch 如何访问异常
【发布时间】:2011-01-12 01:33:24
【问题描述】:

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

这可能吗?

【问题讨论】:

    标签: powershell try-catch


    【解决方案1】:

    试试这样的:

    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

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 1970-01-01
      • 2011-04-01
      • 2013-03-09
      • 2015-06-21
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多