Skip to content

How to handle errors from CLI command in PowerShell #6590

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

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

Comment options

You must be logged in to vote
2 replies
@martinlingstuyl
Comment options

@martinlingstuyl
Comment options

Comment options

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

@Adam-it
Comment options

@MartinM85
Comment options

@Adam-it
Comment options

Answer selected by MartinM85
Comment options

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