本文介绍了Uncatchable异常(PowerShell)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可靠地生成异常的代码。这是预期的,所以我不希望它在脚本结束时显示,当我转储$错误变量来查找实际的问题。



第1步是找到这个异常并处理它,对吧?我不能那么远这是我所得到的:

 功能添加PowerShellSnapIn($ SnapInName){
尝试{
如果((Get-PSSnapin -Name $ SnapInName)-eq $ null){
写入警告SnapIn未加载
}
}捕获[System.Exception] {
写入警告错误捕获
}
}

添加PowerShellSnapIn -SnapInNameMicrosoft.Exchange.Management.PowerShell.Admin

如果我运行这个代码,我可以看到异常,但是我从来没有看到我的一些写警告测试消息, Catch块捕获到异常。我一定在这里遗漏的东西。以下是我看到的例外:



Get-PSSnapin:没有匹配模式Microsoft.Exchange.Management.PowerShell.Admin的Windows PowerShell管理单元找到。检查模式,然后再次尝试该命令。
在C:\users\myuser\Desktop\Test.ps1:4 char:20
+ if((Get-PSSnapin<<< -Name $ SnapInName) -eq $ null){
+ CategoryInfo:InvalidArgument:(Microsoft.Excha ... owerShell.Admin:String)[Get-PSSnapin],PSArgumentException
+ FullyQualifiedErrorId:NoPSSnapInsFound,Microsoft.PowerShell.Commands。 GetPSSnapinCommand



编辑:提前感谢任何花时间帮助我的人!

解决方案

您应该将 -ErrorAction stop 添加到您的 Get-PSSnapin 中以获取进入抓住块。

 功能添加PowerShellSnapIn($ SnapInName){
尝试{
if((Get-PSSnapin - 命名$ SnapInName -ErrorAction Stop)-eq $ null){
写入警告SnapIn未加载
}
}捕获[System.Exception] {
Write-警告错误捕获
}
}

添加PowerShellSnapIn -SnapInNameMicrosoft.Exchange.Management.PowerShell.Admin


I have code that reliably generates an exception. This is expected so I don't want it to show up at the end of my script when I dump the $error variable to look for actual problems.

Well step 1 is to find this exception and handle it, right? I can't get that far. Here is what I've got:

Function Add-PowerShellSnapIn($SnapInName){
    Try{
        if ((Get-PSSnapin -Name $SnapInName) -eq $null){
            Write-Warning "SnapIn Is Not Already Loaded"
        }
    }Catch [System.Exception]{
        Write-Warning "Error Caught"
    }
}

Add-PowerShellSnapIn -SnapInName "Microsoft.Exchange.Management.PowerShell.Admin"

If I run this code I can see the exception, but I never see my little "Write-Warning" test message to indicate that the Catch block caught the exception. I must be missing something here. Here is the exception I see:

Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern 'Microsoft.Exchange.Management.PowerShell.Admin' were found. Check the pattern and then try the command again.At C:\users\myuser\Desktop\Test.ps1:4 char:20+ if ((Get-PSSnapin <<<< -Name $SnapInName) -eq $null){ + CategoryInfo : InvalidArgument: (Microsoft.Excha...owerShell.Admin:String) [Get-PSSnapin], PSArgumentException + FullyQualifiedErrorId : NoPSSnapInsFound,Microsoft.PowerShell.Commands.GetPSSnapinCommand

Edit: Thanks in advance for anyone who takes the time to help me out!

解决方案

You should add -ErrorAction stop to your Get-PSSnapin to get into the Catch Block.

Function Add-PowerShellSnapIn($SnapInName){
    Try{
        if ((Get-PSSnapin -Name $SnapInName -ErrorAction Stop) -eq $null){
            Write-Warning "SnapIn Is Not Already Loaded"
        }
    }Catch [System.Exception]{
        Write-Warning "Error Caught"
    }
}

Add-PowerShellSnapIn -SnapInName "Microsoft.Exchange.Management.PowerShell.Admin"

这篇关于Uncatchable异常(PowerShell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:16