pspete / psPAS

PowerShell module for CyberArk Privileged Access Security REST API
https://pspas.pspete.dev
MIT License
293 stars 91 forks source link

pspete powershell scripting how to show the correct error message #215

Closed jesaldosa closed 4 years ago

jesaldosa commented 4 years ago

10.9

Hope you guys can help.

we have the following bit of code to add accounts from a CSV file using pspete powershell module


try{
    Add-PASAccount -secretType Password -secret $Password -SafeName $Account.SafeName -PlatformID $Account.PlatformID -Address $Account.Address -Username $Account.Username -platformAccountProperties $PlatArray 

}

catch{

    Write-Host "Unable to added group " $SafeName "Error Message:" $_

    Write-Host "Error Message:" $_.Exception.Message
    Write-Host "Error Message:" $_

}

If the safe or platform does not exit it throws the following exception.

Error Message

If you run the same through the web api

Error Message via API

You get the full error message i.e platform not found. How can i get the correct error message from the Powershell script?

what changes are needed to the catch statement to make this work. Any help will be greatly appreciated.

Regards Jesal

jesaldosa commented 4 years ago

Error Message via API Error Message

jesaldosa commented 4 years ago

Added in the error messaged from the script and calling it directly from the webapi

pspete commented 4 years ago

Hi @jesaldosa

I reproduced your scenario, and the message string you want can be obtained in the catch block like this:


$_.TargetObject.ErrorDetails.Message | ConvertFrom-Json | Select-Object -ExpandProperty Details
jesaldosa commented 4 years ago

Thanks Pete. Ill give this a try tomorrow and let you know.

Cheers

jesaldosa commented 4 years ago

Thanks Pete you are a super star

image

That worked! thank you