无法 运行 Azure Runbook 上的 Az 命令

Not able to run Az commands on Azure Runbook

我正在尝试 运行 PowerShell 中的 Az 命令键入 Azure Runbook。启动时它无法识别 Az 命令并要我安装 NuGet。现在安装 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"

错误

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

多个错误

如果您注意到我的 Connect-AzAccount 运行 成功但 Get-AzResource 抛出错误。

  1. 是否需要安装NuGet?
  2. 我的 Connect-AzAccount 如何不抛出错误。
  3. 我的 Uninstall-AzureRm 失败了,但如果我不使用它,它会抛出一个不同的错误。

    Get-ChildItem:AzureRM.Profile 已加载。 Az 和 AzureRM 模块不能在同一个会话中导入或在同一个脚本或 运行book 中使用。

  4. Get-AzResource 是否需要导入其他模块?

  5. 我的本地机器没有问题。仅在 运行 书中有问题。

runbook环境与本地不同,如需使用Get-AzResource,请按以下步骤操作

注意:请确保您在创建自动化帐户时创建了 Run As Account,如下所示。

1.Navigate 到门户中的自动化帐户 -> Modules -> Browse Gallery -> 搜索 Az.AccountsAz.Resources -> Import 他们两个。最新版本Az.Resources依赖Az.Accounts>=1.8.0,如果你已经有旧版本Az.Accounts,只需删除它并安装最新版本,然后安装Az.Resources.

2.Then 在 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>"