|
I'm trying to call a CLI command from the PowerShell script like But due to missing permissions it fails and the output is only error message
Is there any way to check if the command has failed? |
Answered by
Adam-it
Feb 4, 2025
Replies: 3 comments 6 replies
|
If I'm not mistaken, PowerShell struggles to handle stderr output. A function like this can help: function Invoke-CliM365Command {
param (
[Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)]
[string]$command
)
$output = Invoke-Expression "$command 2>&1"
if ($LASTEXITCODE -ne 0) {
throw $output
}
if ($output) {
return $output | ConvertFrom-Json
}
}Then you can use it like: $sites = Invoke-CliM365Command "m365 spo site list"@martinlingstuyl, correct me if I'm wrong or forgot something 🙂 |
2 replies
|
same as @martinlingstuyl I do the following in my scripts |
4 replies
Answer selected by
MartinM85
|
@milanholemans @martinlingstuyl @Adam-it I will try them out to see what suits me best. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

same as @martinlingstuyl I do the following in my scripts