Skip to content

Little addon for people who use azurecli in powershell #32238

@aeternuspolyphagus

Description

@aeternuspolyphagus
function Invoke-AzureCommand {
  param (
    [string]$Command
  )
  $azargs = @($Command -split " ")
  try {
    $stdout = @()
    $stderr = @()
        
    & "C:\Program Files\Microsoft SDKs\Azure\CLI2\python.exe" -IBm azure.cli $azargs 2>&1 | ForEach-Object {
      if ($_ -is [System.Management.Automation.ErrorRecord]) {
        $stderr += $_
      }
      else {
        $stdout += $_
      }
    }
    if ($stderr.Count -gt 0 -and $stdout.Count -eq 0) {
      throw ($stderr -join "`n")
    }
    return $stdout
  }
  catch {
    Write-Error "Azure CLI Error: $($_.Exception.Message)"
  }
}

Use like Invoke-AzureCommand -Command "account list"
With this you can work with try-catch in powershell for automatization.

Metadata

Metadata

Assignees

Labels

Azure CLI TeamThe command of the issue is owned by Azure CLI teamcustomer-reportedIssues that are reported by GitHub users external to the Azure organization.questionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions