Common/Error/Error.psm1
function Show-RMError { param( [Parameter(Mandatory)] [System.Management.Automation.ErrorRecord] $ErrorObj ) $ErrorString = $ErrorObj.ToString() if($ErrorString.Contains("validation_errors")) { $ErrorString = $ErrorString | ConvertFrom-Json foreach($Message in $ErrorString.validation_errors.errors) { if ($null -eq $Message.message) { Write-Error $ErrorObj.ToString() break } else { Write-Error $Message.message } } } elseif ($ErrorString.Contains("message") -And $ErrorString.Contains("resolution")) { $ErrorStringJson = $ErrorString | ConvertFrom-Json if ($null -ne $ErrorStringJson.message -and $null -ne $ErrorStringJson.resolution) { Write-Error $ErrorStringJson.message Write-Error $ErrorStringJson.resolution } elseif ($null -ne $ErrorStringJson.message) { if ($ErrorStringJson.message -eq "Authentication is required to view resource.") { throw "Authentication credentials have expired, please re-login using cmdlet 'Invoke-RMLogin'" } Write-Error $ErrorStringJson.message } else { Write-Error $ErrorObj.ToString() } } else { Write-Error $ErrorString } } Export-ModuleMember -Function Show-RMError |