这是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());
}
这个是可能的吗?
发布于 2010-02-02 16:47:59
尝试如下所示:
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
。
https://stackoverflow.com/questions/2182666
复制相似问题