Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 有没有办法通过自定义System.Management.Automation.ErrorRecord抛出?_Powershell - Fatal编程技术网

Powershell 有没有办法通过自定义System.Management.Automation.ErrorRecord抛出?

Powershell 有没有办法通过自定义System.Management.Automation.ErrorRecord抛出?,powershell,Powershell,我想抛出自定义System.Management.Automation.ErrorRecord,并附带一些附加属性:ContextMessage和SqlCmdHeader 我试图创建继承自System.Management.Automation.ErrorRecord的新类: 类SqlRequestException:System.Management.Automation.ErrorRecord { [哈希表]$SqlCmdHeader [字符串]$ContextMessage SqlRequ

我想抛出自定义
System.Management.Automation.ErrorRecord
,并附带一些附加属性:
ContextMessage
SqlCmdHeader

我试图创建继承自
System.Management.Automation.ErrorRecord的新类:

类SqlRequestException:System.Management.Automation.ErrorRecord { [哈希表]$SqlCmdHeader [字符串]$ContextMessage SqlRequestException(){} SqlRequestException( [字符串]$message, [哈希表]$sqlCmdHeader, [System.Management.Automation.ErrorRecord]$ErrorRecord ):base($errorRecord,$null) { $this.SqlCmdHeader=$SqlCmdHeader $this.ContextMessage=$message } }
但当我通过以下代码抛出错误时:

试试看
{
#这里出现了例外情况
}
抓住
{
抛出([SqlRequestException]::新建(
“一些异常上下文消息将有助于理解错误,而无需额外调试代码”,
$sqlHeader,
$_))
}
我刚接到来电者
系统。管理。自动化。错误记录
,没有我的精彩属性。看起来像是
throw
将我的对象强制转换为
System.Management.Automation.ErrorRecord
并丢失了属性

我想请你们帮我在我的脚本中实现这个逻辑。任何帮助都将不胜感激

更新 我无法更改呼叫者的代码。它总是分析错误并将其写入日志:

试试看
{
#从我试图抛出自定义ErrorRecord的模块调用方法
}
抓住
{
写日志文本$_
}
我无法使用
System.Exception
,因为当我抛出创建的异常时,
ErrorRecord
中的所有原始信息(如
InvocationInfo
ScriptStackTrace
等)都会丢失。我找不到
throw
语句的源代码来理解它到底是如何工作的(也许有人能给我指一下?)


我看到有
TargetObject
属性可以用来存储一些附加信息,但是我看不到在
throw
语句之前向属性添加内容的方法,因为属性是
ReadOnly
,我认为该属性不是为存储额外的用户数据而设计的。

您可以在catch块中创建自定义对象(您不需要自己的类),类似这样的

$customobj = New-Object -TypeName psobject 
$customobj | Add-Member -MemberType NoteProperty -Name SQLhead -Value $sqlHeader 
$customobj | Add-Member -MemberType NoteProperty -Name Error -Value $_
关于创建自定义对象的更多信息

虽然类在技术上没有密封,文档中也没有提到子类化,但实际上似乎不支持子类化
ErrorRecord

正如您所经历的,您抛出的原始
SqlRequestException
实例并不构成调用堆栈,这可能是因为需要复制到基类的实例中

改为子类
系统.Exception
,并让PowerShell自动将您的异常包装在
[ErrorRecord]
实例中,该实例的
.Exception
属性稍后您可以查询:

# Make your custom exception derive from System.Exception.
class SqlRequestException : System.Exception
{
    [hashtable]$SqlCmdHeader
    [string]$ContextMessage

    # Construct the exception with the one passed as an 
    # argument as the *inner* exception.
    SqlRequestException(
        [string]$contextMessage,
        [hashtable]$sqlCmdHeader,
        [exception]$innerException
    ) : base($innerException.Message, $innerException)
    {
        $this.ContextMessage = $contextMessage
        $this.SqlCmdHeader = $sqlCmdHeader
    }
}

try { # Outer `try` to simulate a caller.
  try # Your code.
  {
      [int]::Parse('not a number') # Sample exception.
  }
  catch
  {
     # Throw your custom exception that wraps the original exception
     # ($_.Exception) via its .InnerException property.
     # PowerShell itself then automatically wraps your custom exception in
     # an [ErrorRecord] instance.
     throw ([SqlRequestException]::New(
              'custom message',
              @{ header1 = 'foo' },
              $_.Exception))
  }
} catch { # The (simulated) caller's catch block.
  # Now you can access your custom properties via $_.Exception.
  @"
Error: $_
Custom properties:
  ContextMessage: $($_.Exception.ContextMessage)
  SqlCmdHeader: $($_.Exception.SqlCmdHeader | Out-String)
"@
}
上述结果显示,这两个自定义属性都已传递:

错误:使用“1”参数调用“Parse”时出现异常:“输入字符串的格式不正确。”
自定义属性:
ContextMessage:自定义消息
SqlCmdHeader:
名称值
----                           -----
一富校长

自定义异常的消息是原始异常的消息;要获取原始异常的详细信息,请访问
$\u0.InnerException

@acelighting:澄清:更新主要按照您的需要进行,除了原始异常现在是自定义异常的
.InnerException
,尽管消息是原始异常。如果这还不够,请提供反馈。