question

PerumalJanakiraman-5353 avatar image
1 Vote"
PerumalJanakiraman-5353 asked JoshC24 edited

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

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
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @PerumalJanakiraman-5353,

AFAIK, currently it is not a supported feature. I see these related feature requests already raised and one of them is triaged by responsible Azure feature team. So I recommend you to upvote it. In general, Azure product or feature team would check feasibility of a feature request, prioritize against existing feature backlog, add in roadmap as appropriate and would announce and/or update the related Azure document once a feature request is addressed.


0 Votes 0 ·

If it's okay I would recommend you to try PowerShell equivalent of your CLI command. Example:, if the CLI command is az vm run-command invoke then try to go for Az PowerShell cmdlet Invoke-AzVMRunCommand.


0 Votes 0 ·

I have tried this option, but i noticed the script path of the command checks for the location on the sandbox where its running.

For example if i want to invoke a script on remote server, Invoke-AzVMRuncommand checks that locally and fails with the error file is not found.

Regards,
Jana.

0 Votes 0 ·
tbgangav-MSFT avatar image
0 Votes"
tbgangav-MSFT answered tbgangav-MSFT commented

Hi @PerumalJanakiraman-5353,

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @PerumalJanakiraman-5353,

Did you get chance to review the above information? Let me know if you have any further queries regarding it.

0 Votes 0 ·
janakiramanperumal-4402 avatar image
1 Vote"
janakiramanperumal-4402 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MichealFalowo-8474 avatar image
0 Votes"
MichealFalowo-8474 answered venkatprabhu-2777 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

you are awesome

0 Votes 0 ·
JoshC24 avatar image
3 Votes"
JoshC24 answered JoshC24 edited

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey @JoshC24 - I have the same problem. Did you get this resolved?

0 Votes 0 ·
JoshC24 avatar image JoshC24 DamianEvans-2262 ·

@DamianEvans-2262 I did not. I ended up just running my script as a scheduled task on a VM I had in enrolled into Azure Arc and authenticated in the script using a managed identity for the VM. Not ideal but works for now.

0 Votes 0 ·