Use Az.Subscription in Azure Automation

Wellington Adilson Dorado 0 Reputation points
2024-12-17T18:11:42.7233333+00:00

Hello, I am trying to use some Azure commands in my Automation Account, but I am getting an error. The code performs the following function: it logs into Azure and disables the subscription using the command Disable-AzSubscription, which is part of the Az.Accounts and/or Az.Subscription module. I created an Automation Account and added a runbook with a PowerShell script. This script contains the disable and enable commands for the subscription. However, when I run the test for the runbook, it returns that the "az" command is not recognized and asks me to install the AZ modules. I have already tried importing them through the Automation Account menu in the portal and also tried importing via the command line, but I get the error "This command is part of the Az.Subscription module, but it is not installed." The other error I get is that the "az" command is not recognized. What should I do in this situation?

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,278 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,708 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ashok Gandhi Kotnana 1,580 Reputation points Microsoft Vendor
    2024-12-20T12:32:50.4133333+00:00

    Hi @Wellington Adilson Dorado

    Welcome to Microsoft Q&A Forum, thank you for posting your query here!

    Please follow below instructions this will resolve your issue.

    1. Please enable managed identity of your automation account keep it On
      User's image
    2. Once you have enabled provide this managed identity copy paste the identity in IAM and provide that identity with contributor access find the below screenshot, please provide it to the appropriate managed identity
      User's image
    3. Please add the below script before you run any commands
    
    #Connect to Azure with system-assigned managed identity
     
    $AzureContext = (Connect-AzAccount -Identity).context
     
    # set and store context
     
    $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
    

    User's image

    My example i have used get-azvm you can use whatever the commands you want to, please find the below output screenshot for azvm

    User's image

    let us know if you have any further queries. I’m happy to assist you further.  

    Please provide your valuable comments User's image

    Please do not forget to "Accept the answer” wherever the information provided helps you, this can be beneficial to other community members.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.