Skip to content

How to handle errors from CLI command in PowerShell #6590

Answered by Adam-it
MartinM85 asked this question in Q&A

You must be logged in to vote

same as @martinlingstuyl I do the following in my scripts

m365 cli config set --key output --value json
m365 cli config set --key errorOutput --value stdout
m365 cli config set --key showHelpOnFailure --value false
m365 cli config set --key printErrorsAsPlainText --value false

function Get-CLIValue {
    [cmdletbinding()]
    param(
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $input
    )
    $output = $input | ConvertFrom-Json
    if ($null -ne $output.error) {
        throw $output.error
    }
    return $output
}

try {
    # some script here like 
    m365 spo list list --webUrl "random" | Get-CLIValue 
}
catch {
    Write-Host "Errors: $($_.Exception.Me…

Replies: 3 comments 6 replies

You must be logged in to vote
2 replies
@martinlingstuyl

@martinlingstuyl

You must be logged in to vote
4 replies
@Adam-it

@Adam-it

@MartinM85

@Adam-it

Answer selected by MartinM85

You must be logged in to vote
0 replies
# for free to join this conversation on GitHub. Already have an account? # to comment
Category
Q&A
Labels
None yet
4 participants