0

我正在尝试在 PowerShell 类型 Azure Runbook 中运行 Az 命令。启动时它无法识别 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 模块不能在同一会话中导入,也不能在同一脚本或 Runbook 中使用。

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

  5. 我的本地机器上没有问题。仅在运行手册中有问题。
4

1 回答 1

4

Runbook 的环境与本地环境不同,如果要使用Get-AzResource,请按照以下步骤操作。

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

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

于 2020-05-20T04:22:40.240 回答