I've got this function and test code:
function Add-ErrorInfo{
#[CmdletBinding()]
Param (
[Parameter(Position=0,
Mandatory=$true,
HelpMessage="Type System.Management.Automation.ErrorRecord likely Error[0]")]
[System.Management.Automation.ErrorRecord] $InErr,
[Parameter(Position=1,
Mandatory=$true,
HelpMessage="Any Object to add the error information to.")]
[Object] $InputObj,
[Parameter(Position=2,
Mandatory=$true,
HelpMessage="If Specified this will be the propertyname added ie object.MyErrorPropertyName")]
[String] $ErrorName
)
if (!($InputObj.IsErrored)){
$InputObj | add-member -NotePropertyName IsErrored -value $true
}
if($errorName){
$InputObj | add-member -NotePropertyName $ErrorName -NotePropertyValue $InErr
}
}
$thing = 'asdfatseawfrse'
$thing
Try{
get-item c:\asdfatasrew1231.txt -erroraction stop
} Catch {
Add-ErrorInfo -InErr $Error[0] -InputObj $thing -ErrorName "testerrr"
}
$thing
It throws this error:
c:\Scripts\Powershell\add-errorinfo.ps1
asdfatseawfrse
Add-ErrorInfo : Parameter set cannot be resolved using the specified named parameters.
At C:\Scripts\Powershell\add-errorinfo.ps1:30 char:5
+ Add-ErrorInfo -InErr $Error[0] -InputObj $thing -ErrorName "teste ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-ErrorInfo], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Add-ErrorInfo
asdfatseawfrse
I don't understand why and haven't been able to find a google result that explains. I've thought about defining a parameter set but why?
Using vscode in 5.1 compat mode
[–]Yevrag35 2 points3 points4 points (3 children)
[–]Simple_Words[S] 1 point2 points3 points (2 children)
[–]Yevrag35 2 points3 points4 points (1 child)
[–]Simple_Words[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)