I have some code in a powershell function that looks like this:
try {
Invoke-SomethingThatFails
}
catch [System.Exception] {
Write-Error $_.Exception.Message;
return New-Object Namespace.CustomErrorType -Property @{
'Code' = "Fail";
"Message" = $_.Exception.Message;
}
}
Now the only problem is I’d also like to set $? to be $false. Is this possible?
From Bruce Payettes’ PowerShell In Action (Second Edition):
The PowerShell runtime manages the value of
$?and is set to false when an error object gets written in the pipeline.Update Here is how you can write an error object to the pipeline but not terminate it (the pipeline):
Output: