Invoking AZ CLI commands within powershell script in Azure runbook for automation

Perumal, Janakiraman 241 Reputation points
2020-11-11T13:28:08.837+00:00

Hi Guys,

Is it possible to run a AZ Cli command within my powershell runbook in Azure?

From Azure cloud shell i am able to execute the CLI commands to perform some tasks on my remote machine within azure network. I want to incorporate the same commands within my Powershell runbook.

az --vm run-command invoke --command-id RunShellScript --resource-group 'AUTOMATION_TESTRG' --name 'testing-vm-stripe' --scripts '/tmp/one.sh'

Regards,

Janakiraman

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,113 questions
{count} vote

4 answers

Sort by: Most helpful
  1. JoshC 16 Reputation points
    2021-11-22T22:26:56.337+00:00

    Has anything changed with this regarding Azure CLI in PS 7.0 (preview) in the last couple days? I've been able to run Azure CLI in runbooks for the last 3 weeks or so, using it to export our Azure DNS zones to txt files then using Azure Powershell to copy the txt files to a storage account.

    This script ran every day at 5am and looks to have started failing on 11/20 and no longer recognizes the "az login" command when running. Nothing else has changed on this automation account, still using PS 7.0 (preview) runtime.

    3 people found this answer helpful.

  2. janakiraman perumal 6 Reputation points
    2020-11-17T01:01:42.387+00:00

    Hello tbgangav-MSFT,

    I have tried to invoke the remote commands to api calls and it worked for me yesterday, apologize for the delay in response.

    Here is the link which i followed.

    Thanks again for your support.

    https://blog.tekspace.io/access-azure-rest-api-using-powershell/

    Regards,
    Janakiraman

    1 person found this answer helpful.
    0 comments No comments

  3. Micheal Falowo 6 Reputation points
    2021-05-02T22:20:21.043+00:00

    Yes you can run az cli in PowerShell.

    Once az cli is installed, add az cli command path to your computer's environmental variable and then set the alias for az following below steps.

    1. Run below commands as an administrator. Adding az cli command path to your computer's environmental variable path $azCliPath = "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin"
      $CurrentPath =(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
      $NewPath = “$CurrentPath;$azCliPath” Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $NewPath
    2. Set alias for az command running using below PowerShell command Set-Alias -Name az -Value "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd"

    After completing the above steps, you should be able to run "az" command in your PowerShell or ISE.

    1 person found this answer helpful.

  4. tbgangav-MSFT 10,381 Reputation points
    2020-11-13T11:25:26.933+00:00

    Hi @Perumal, Janakiraman ,

    Invoke script (for example test.ps1 that's under c:\ drive) on a remote server with the help of below approach.

    $ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'  
    Add-AzAccount -ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint  
    $rgname ="rrrrrrrrrrrrrr"  
    $vmname ="vvvvvvvvvvvvvv"  
    $ScriptToRun = "c:\test.ps1"  
    Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1   
    Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1  
    Remove-Item -Path ScriptToRun.ps1  
    

    Note: Replace value of $rgname and $vmname with your resource group name and VM name as appropriate.