【发布时间】:2011-06-09 04:16:05
【问题描述】:
为什么我在运行这两个简单示例时会在控制台上打印错误消息? 我希望在控制台上打印“错误测试:)”:
Get-WmiObject : RPC 服务器是 不可用。 (HRESULT 的例外情况: 0x800706BA) 在行:3 字符:15 + Get-WmiObject
或
试图除以零。在线:3 字符:13 + $i = 1/ + CategoryInfo : 未指定: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : RuntimeException
第一个例子:
try
{
$i = 1/0
Write-Host $i
}
catch [Exception]
{
Write-Host "Error testing :)"
}
第二个例子:
try
{
Get-WmiObject -ComputerName possibly.nonexisting.domain.com -Credential (Get-Credential) -Class Win32_logicaldisk
}
catch [Exception]
{
Write-Host "Error testing :)"
}
非常感谢!
【问题讨论】:
标签: powershell exception-handling powershell-2.0