powershellnuget-packageazure-powershellazure-runbookaz

Not able to run Az commands on Azure Runbook


I am trying to run the Az commands inside PowerShell type Azure Runbook. At starting it does not recognize the Az commands and want me to install NuGet. Now error displayed while installing NuGet.

#Set strong cryptography on 64 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

#Set strong cryptography on 32 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord 

#Install NuGet
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

#Uninstall AzureRm
Uninstall-AzureRm

#Install Module
Install-Module -Name Az.Accounts -Force
Install-Module -Name Az.Resources -Force

#Import Module
Import-Module -Name Az.Accounts -Force
Import-Module -Name Az.Resources -Force

#Connect to your Azure Account
$Account = Connect-AzAccount -Credential $Cred

Get-AzResource -ResourceGroupName "test"

Error

Install-PackageProvider : No match was found for the specified search criteria for the provider 'NuGet'. The package provider requires 'PackageManagement' and 'Provider' tags. Please check if the specified package has the tags. At line:17 char:1 + Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (Microsoft.Power...PackageProvider:InstallPackageProvider) [Install-PackageProvider], Exception + FullyQualifiedErrorId : NoMatchFoundForProvider,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackageProvider

Multiple Errors enter image description here

If you notice that my Connect-AzAccount run successfully but Get-AzResource throw error.

  1. Is it necessary to install NuGet?
  2. How my Connect-AzAccount does not throw an error.
  3. My Uninstall-AzureRm is failing but if I don't use it then it throws a different error.

    Get-ChildItem : AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook.

  4. Is Get-AzResource required some other module to import?

  5. There is no issue at my local machine. Having an issue in runbook only.

Solution

  • The environment of runbook is different from that in local, if you want to use Get-AzResource, please follow the steps below.

    Note: Please make sure you created the Run As Account when you created the automation account like below.

    enter image description here

    1.Navigate to the automation account in the portal -> Modules -> Browse Gallery -> search for Az.Accounts and Az.Resources -> Import both of them. The latest version of Az.Resources has a dependency of Az.Accounts>=1.8.0, if you have already have the old version of Az.Accounts, just remove it and install the newest version, then install the Az.Resources.

    enter image description here

    2.Then use the script below in the powershell runbook.

    $connectionName = "AzureRunAsConnection"
    try
    {
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         
    
        Connect-AzAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
    }
    catch {
        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
    }
    
    Get-AzResource -ResourceGroupName "<group-name>"
    

    enter image description here