[英]powershell 2.0 try catch how to access the exception
This is the try catch
in PowerShell 2.0 这是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")
}
}
but what I want to do is something like in c# 但我想要做的就是在c#中
catch( WebException ex)
{
Log(ex.ToString());
}
Is this possible? 这可能吗?
Try something like this: 尝试这样的事情:
try {
$w = New-Object net.WebClient
$d = $w.downloadString('http://foo')
}
catch [Net.WebException] {
Write-Host $_.Exception.ToString()
}
The exception is in the $_
variable. $_
变量中有例外。 You might explore $_
like this: 您可以像这样探索
$_
:
try {
$w = New-Object net.WebClient
$d = $w.downloadString('http://foo')
}
catch [Net.WebException] {
$_ | fl * -Force
}
I think it will give you all the info you need. 我想它会为您提供所需的所有信息。
My rule: if there is some data that is not displayed, try to use -force
. 我的规则: 如果有一些数据没有显示,请尝试使用
-force
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.