如何以及何时使用ErrorDetails.RecommendedAction属性?默认情况下似乎不会使用它:
using namespace System
using namespace System.Management.Automation
$exception = New-Object Exception -ArgumentList "This is the Exception message"
$errorRecord = New-Object ErrorRecord -ArgumentList $exception, "This is the ErrorRecord Message", NotSpecified, $null
$errorDetails = New-Object ErrorDetails -ArgumentList "This is the ErrorDetails message"
$errorRecord.ErrorDetails = $errorDetails
$errorDetails.RecommendedAction = "This is the recommended action"
Write-Error $errorRecord
: This is the ErrorDetails message
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException发布于 2020-10-04 22:08:49
引擎AFAIK中的任何内容都不使用它。
但是,您可以将其用于您自己的目的!
我现在有一个问题要解决,任务可能会因为请求的更改已经存在而失败-例如,添加一个用户帐户,但该帐户已经存在。
我们的编码风格要求我们发出一个(非终止的)错误,这符合项目已经存在时的行为,比如New-Item。
但是我们的上游自动化不应该将票证移动到队列中以进行进一步的操作-默认情况下,它会在发生错误时这样做。
在我们的代码中,我们可以这样做
Write-Error 'whatever error msg' -RecommendedAction 'NoFurtherAction'
上游客户端可以根据ErrorDetails中的这个字符串过滤工单操作:
$Error[0].ErrorDetails.RecommendedAction -eq 'NoFurtherAction'
https://stackoverflow.com/questions/34675946
复制相似问题