PowerShell 2.0以及如何处理异常? [英] PowerShell 2.0 and how to handle exceptions?

查看:138
本文介绍了PowerShell 2.0以及如何处理异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在运行这两个简单的样本时会在控制台上打印出错误信息?
我想要在控制台上打印出我的错误测试:):


Get-WmiObject:RPC服务器是
不可用。 (HRESULT的异常:
0x800706BA)在行:3 char:15
+ Get-WmiObject<<<< -ComputerName might.nonexisting.domain.com
-Credential(Get-Credential) - 类Win32_logicaldisk
+ CategoryInfo:InvalidOperation:(:) [Get-WmiObject],
COMException
+ FullyQualifiedErrorId:GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand



尝试除以零。在行:3
char:13
+ $ i = 1 /<<<< 0

+ CategoryInfo:NotSpecified:(:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId:RuntimeException


第一个例子:

  try 
{
$ i = 1/0
写主机$ i
}
catch [异常]
{
写主机错误测试:)
}

第二个例子:

  try 
{
Get-WmiObject -ComputerName might.nonexisting.domain.com -Credential(Get-Credential)-Class Win32_logicaldisk
}
catch [Exception]
{
写主机错误测试:)
}

非常感谢

解决方案

第一个例子



<在编译/解析时出现错误(PowerShell足够聪明),因此代码甚至不执行,也无法捕获任何东西。尝试这个代码,你会得到一个例外:

  try 
{
$ x = 0
$ i = 1 / $ x
写主机$ i
}
catch [异常]
{
写主机错误测试:)
}






第二个例子



如果您在全局设置 $ ErrorActionPreference ='Stop',那么您将获得错误测试: )按预期印刷。但是你的 $ ErrorActionPreference 可能是'继续':在这种情况下,没有终止错误/异常,你只是得到



而不是全局 $ ErrorActionPreference 选项,您可以还可以使用 Get-WmiObject 参数 ErrorAction 。尝试将其设置为停止,您将收到一个例外。

 尝试
{
Get-WmiObject -ErrorAction Stop -ComputerName possible.nonexisting.domain.com -Credential(Get-Credential) - 类Win32_logicaldisk
}
catch [Exception]
{
写主机错误测试:)
}


Why I get error message printed on the console when running these two simple samples ? I want that I get "Error testing :)" printed on the console insted of:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) At line:3 char:15 + Get-WmiObject <<<< -ComputerName possibly.nonexisting.domain.com -Credential (Get-Credential) -Class Win32_logicaldisk + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

or

Attempted to divide by zero. At line:3 char:13 + $i = 1/ <<<< 0
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : RuntimeException

First example:

try
{
    $i = 1/0   
    Write-Host $i     
}
catch [Exception]
{ 
    Write-Host "Error testing :)" 
}

Second example:

try
{
    Get-WmiObject -ComputerName possibly.nonexisting.domain.com -Credential (Get-Credential) -Class Win32_logicaldisk 
}
catch [Exception]
{ 
    Write-Host "Error testing :)" 
}

Thank you very much!

解决方案

First example

The error happens at compile/parsing time (PowerShell is clever enough), so that the code is not even executed and it cannot catch anything, indeed. Try this code instead and you will catch an exception:

try
{
    $x = 0
    $i = 1/$x
    Write-Host $i
}
catch [Exception]
{
    Write-Host "Error testing :)"
}


Second example

If you set $ErrorActionPreference = 'Stop' globally then you will get "Error testing :)" printed, as expected. But your $ErrorActionPreference is presumably 'Continue': in that case there is no terminating error/exception and you just get the non terminating error message printed to the host by the engine.

Instead of the global $ErrorActionPreference option you can also play with Get-WmiObject parameter ErrorAction. Try to set it to Stop and you will catch an exception.

try
{
    Get-WmiObject -ErrorAction Stop -ComputerName possibly.nonexisting.domain.com -Credential (Get-Credential) -Class Win32_logicaldisk
}
catch [Exception]
{
    Write-Host "Error testing :)"
}

这篇关于PowerShell 2.0以及如何处理异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆