Public/ErrorHandling/Get-ErrorMessageFromErrorString.ps1
# Module: Orbit.Tools # Function: UserAdmin # Author: David Eberhardt # Updated: 20-DEC-2020 # Status: RC function Get-ErrorMessageFromErrorString { <# .SYNOPSIS Extracts Message from an Error Message returned .DESCRIPTION Error Messages from a Rest API call are ususally displayed as a multi-line string. This CmdLet splits the string and returns the third line (Message) only .PARAMETER Exception String returned by an CmdLet .EXAMPLE Get-ErrorMessageFromErrorString $_ Returns String with the Message only .EXAMPLE $_ | Get-ErrorMessageFromErrorString Returns String with the Message only .INPUTS System.String .OUTPUTS System.String .NOTES Use in a CATCH Block to pass the Exception from TRY to this command. V1 will only return a String with the message V2 may return a better Error-Object .COMPONENT ErrorHandling .FUNCTIONALITY Translates Exception Multi-Line String to Exception Message (single line string) .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/Orbit.Tools/Get-ErrorMessageFromErrorString.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/about/about_Supporting_Functions.md .LINK https://github.com/DEberhardt/Orbit/tree/main/docs/ #> [CmdletBinding()] [OutputType([System.String])] param( [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, HelpMessage = 'Enter the identity of the Admin Account')] [Alias('Message', 'String')] [string]$Exception ) #param begin { #Show-OrbitFunctionStatus -Level Live #Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)" #Write-Verbose -Message "Need help? Online: $global:Orbit.ToolsHelpURLBase$($MyInvocation.MyCommand)`.md" #$Stack = Get-PSCallStack #$Called = ($stack.length -ge 3) # Setting Preference Variables according to Upstream settings if (-not $PSBoundParameters['Verbose']) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') } if (-not $PSBoundParameters['Debug']) { $DebugPreference = $PSCmdlet.SessionState.PSVariable.GetValue('DebugPreference') } else { $DebugPreference = 'Continue' } if ( $PSBoundParameters['InformationAction']) { $InformationPreference = $PSCmdlet.SessionState.PSVariable.GetValue('InformationAction') } else { $InformationPreference = 'Continue' } } #begin process { #Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)" Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand) - Processing ErrorMessage Exception string" $Message = $Exception.Split("`n")[2] Write-Output $Message } #process end { #Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)" } #end } #Get-ErrorMessageFromErrorString |