Skip to content

Instantly share code, notes, and snippets.

@jessitron
Last active Mar 17, 2021
Embed
What would you like to do?
Unwrapping STDERR in Powershell
<#
.Synopsis
STDERR strings get wrapped in an ErrorRecord. Unwrap those back into strings.
#>
function Convert-StderrString {
# Why can this not be an advanced function with [CmdletBinding()] ?
# Somehow it doesn't work if I put that in.
begin {
}
process {
if ($PSItem -is [System.Management.Automation.ErrorRecord]) {
# yeah OK I haven't figured out how to say 'and' yet
if ($PSItem.FullyQualifiedErrorId -eq "NativeCommandError") {
if ($PSItem.TargetObject -is [string]) {
$PSItem.TargetObject
return;
}
}
}
$PSItem
}
end {
}
}
Set-Alias -Name fix-stderr -Value Convert-StderrString
@KacperMucha

This comment has been minimized.

Copy link

@KacperMucha KacperMucha commented Nov 14, 2019

$PSItem -is [System.Management.Automation.ErrorRecord] -and $PSItem.FullyQualifiedErrorId -eq "NativeCommandError" -and $PSItem.TargetObject -is [string]

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment