say I have the following script test-exception.ps1
[CmdletBinding()]
param()
process {
1/0
}
if I run it, I get
Tentative de division par zéro.
Au caractère U:\test-exception.ps1:5 : 2
+ 1/0
+ ~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
if now I try/catch the error,
[CmdletBinding()]
param()
process {
try {
1/0
} catch {
write-error $_
}
}
I get
Au caractère Ligne:1 : 1
+ U:\test-exception.ps1
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test-exception.ps1
where I lose the line number and the line text. I tried many variants but the only way I've found to get the first error message in the error stream of a surrounding CMD while try/catching the code (I want to be able to exit with some custom exit code, not 1) is
[CmdletBinding()]
param()
process {
try {
1/0
} catch {
[Console]::Error.WriteLine(($_ | Out-String))
exit 99
}
}
did I miss something or is this the only solution?