如何在PowerShell中捕获异常并将其发送到方法来处理属性?
我有一个工作原理如下的挂钟:如何在PowerShell中捕获异常并将其发送到方法来处理属性?,powershell,exception-handling,Powershell,Exception Handling,我有一个工作原理如下的挂钟: catch [System.InvalidOperationException] { Process-Exception($_.Exception,1111) } # Write exceptions to eventlog and reset socket function Process-Exception { param ($exception, $eventID) $dateTime = Get-Date #Write-Ev
catch [System.InvalidOperationException]
{
Process-Exception($_.Exception,1111)
}
# Write exceptions to eventlog and reset socket
function Process-Exception { param ($exception, $eventID)
$dateTime = Get-Date
#Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `
#for now: write-host [string] instead of -Message [string]
write-host "Date and time:`t" + $dateTime + "`r`n`"
write-host "Exception Source:`t" + $exception.Source
write-host "Error Code:`t"+ $exception.NativeErrorCode
write-host "Exception Message:" + $exception.Message
write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n`
$socket = $null
}
然后我想这样处理异常:
catch [System.InvalidOperationException]
{
Process-Exception($_.Exception,1111)
}
# Write exceptions to eventlog and reset socket
function Process-Exception { param ($exception, $eventID)
$dateTime = Get-Date
#Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `
#for now: write-host [string] instead of -Message [string]
write-host "Date and time:`t" + $dateTime + "`r`n`"
write-host "Exception Source:`t" + $exception.Source
write-host "Error Code:`t"+ $exception.NativeErrorCode
write-host "Exception Message:" + $exception.Message
write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n`
$socket = $null
}
其中`t
是一个选项卡,需要访问$exception
属性。不幸的是,当我这样做时,异常没有发送到方法,只有错误代码(本例为1111)被发送。信息是:
write-host Stack Trace: + Cannot find type [[System.InvalidOperationException]]:
make sure the assembly containing this type is loaded. 9999.StackTrace +
如何将完整异常发送到该方法?如果你对代码有改进,请告诉我 这和你的情况不完全一样,但我想知道这是否适合你。据我所知,你的代码看起来不错。我对函数及其参数定义做了一些更改
function Process-Exception {
[CmdletBinding()]
param (
[System.Exception] $Exception
, [int] $EventID
)
$dateTime = Get-Date
#Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `
#for now: write-host [string] instead of -Message [string]
write-host "Date and time:`t" + $dateTime + "`r`n";
write-host "Exception Source:`t" + $exception.Source
write-host "Error Code:`t"+ $exception.NativeErrorCode
write-host "Exception Message:" + $exception.Message
write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n"
}
try {
[invalidtype]
}
catch [System.Exception] {
Process-Exception -Exception $_.Exception -EventID 1111;
}
这和你的情况不完全一样,但我想知道这是否适合你。据我所知,你的代码看起来不错。我对函数及其参数定义做了一些更改
function Process-Exception {
[CmdletBinding()]
param (
[System.Exception] $Exception
, [int] $EventID
)
$dateTime = Get-Date
#Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `
#for now: write-host [string] instead of -Message [string]
write-host "Date and time:`t" + $dateTime + "`r`n";
write-host "Exception Source:`t" + $exception.Source
write-host "Error Code:`t"+ $exception.NativeErrorCode
write-host "Exception Message:" + $exception.Message
write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n"
}
try {
[invalidtype]
}
catch [System.Exception] {
Process-Exception -Exception $_.Exception -EventID 1111;
}
这是我刚才玩的一些代码
function Process-Exception
{
param([System.InvalidOperationException]$Exception, [int] $EventID)
$dateTime = Get-Date
#Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `
#for now: write-host [string] instead of -Message [string]
write-host "Date and time:`t" + $dateTime + "`r`n`"
write-host "Exception Source:`t" + $exception.Source
write-host "Error Code:`t"+ $exception.NativeErrorCode
write-host "Exception Message:" + $exception.Message
write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n`
$socket = $null
}
function ExceptionTesting
{
Try
{
throw [System.InvalidOperationException] "Here is a message"
}
catch [System.InvalidOperationException]
{
#Write-Host $_.Exception
Process-Exception $_.Exception 1111
}
}
ExceptionTesting
这是我刚才玩的一些代码
function Process-Exception
{
param([System.InvalidOperationException]$Exception, [int] $EventID)
$dateTime = Get-Date
#Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `
#for now: write-host [string] instead of -Message [string]
write-host "Date and time:`t" + $dateTime + "`r`n`"
write-host "Exception Source:`t" + $exception.Source
write-host "Error Code:`t"+ $exception.NativeErrorCode
write-host "Exception Message:" + $exception.Message
write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n`
$socket = $null
}
function ExceptionTesting
{
Try
{
throw [System.InvalidOperationException] "Here is a message"
}
catch [System.InvalidOperationException]
{
#Write-Host $_.Exception
Process-Exception $_.Exception 1111
}
}
ExceptionTesting
函数中有语法错误,调用错误
# Write exceptions to eventlog and reset socket
function Process-Exception { param ([Exception]$exception, [int]$eventID)
$dateTime = Get-Date
write-host ("Date and time:`t" + $dateTime + "`r`n")
write-host ("Exception Source:`t" + $exception.Source)
write-host ("Error Code:`t"+ $exception.NativeErrorCode)
write-host ("Exception Message:" + $exception.Message)
write-host ("Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n")
$socket = $null
}
在catch块中调用函数的示例:
Process-Exception $_.Exception 111
参考链接:
函数中有语法错误,调用错误
# Write exceptions to eventlog and reset socket
function Process-Exception { param ([Exception]$exception, [int]$eventID)
$dateTime = Get-Date
write-host ("Date and time:`t" + $dateTime + "`r`n")
write-host ("Exception Source:`t" + $exception.Source)
write-host ("Error Code:`t"+ $exception.NativeErrorCode)
write-host ("Exception Message:" + $exception.Message)
write-host ("Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n")
$socket = $null
}
在catch块中调用函数的示例:
Process-Exception $_.Exception 111
参考链接:
如果我错了,请纠正我,但错误表明程序集未加载 找不到类型[[System.InvalidOperationException]]: 确保已加载包含此类型的程序集 请先尝试加载程序集,如果仍然出现相同错误,请通知我:
添加类型-AssemblyName mscorlib
如果我错了,请纠正我,但错误表明程序集未加载 找不到类型[[System.InvalidOperationException]]: 确保已加载包含此类型的程序集 请先尝试加载程序集,如果仍然出现相同错误,请通知我:
添加类型-AssemblyName mscorlib
`您在行上有额外的瓦片来写入日期时间和堆栈跟踪。`哈哈,现在我明白了:-)
调用函数时,您传递的是一个对象数组,而不是所需的两个参数。
hmm。。如何从catchblock获取异常?上述webscholar代码是否有效?(现在在Linux上)我更新了调用函数的示例,向您展示了如何在catch块中获取异常。我想Trevor和我应该指出我们所做的语法更改。@websch01ar-我测试了您的函数,并对未决问题进行了评论。`您在行上有额外的波浪线来写入日期时间,在行上有额外的波浪线来写入堆栈跟踪。`哈哈现在我明白了:-)调用函数时,传递的是单个对象数组,而不是所需的两个参数。
hmm。。如何从catchblock获取异常?上述webscholar代码是否有效?(在Linux now上…)我更新了调用函数的示例,向您展示了如何在catch块中获取异常。我想Trevor和我应该指出我们所做的语法更改。@websch01ar-我测试了您的函数并对未决问题进行了评论。此函数与OP的问题相同。最大的问题是,在写入主机时,异常属性没有扩展。我明白您在这里所说的,但是写入主机语句不是影响OP的核心问题。我同意,我没有投你反对票——我只是想指出我在OP上看到的所有错误,以便更好地帮助他们理解实现预期结果所需的更改。要了解基本的异常详细信息,你会使用Trace吗?我知道PowerShell在这之前去掉了很多异常细节。但是,您可以捕获该特定错误,然后创建一个包含一些注释细节的自定义PSObject。但对我来说,这似乎是一个黑客攻击。我能不能让它成为所有异常的通用函数?这个函数与OP的问题相同。最大的问题是,在写入主机时,异常属性没有扩展。我明白您在这里所说的,但是写入主机语句不是影响OP的核心问题。我同意,我没有投你反对票——我只是想指出我在OP上看到的所有错误,以便更好地帮助他们理解实现预期结果所需的更改。要了解基本的异常详细信息,你会使用Trace吗?我知道PowerShell在这之前去掉了很多异常细节。但是,您可以捕获该特定错误,然后创建一个包含一些注释细节的自定义PSObject。但这对我来说似乎是一种攻击。我能不能让它成为所有异常的通用函数?