无法在Azure Runbook上运行Az命令

我正在尝试在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

多个错误

无法在Azure Runbook上运行Az命令

如果您发现我的 Connect-Azaccount 成功运行,但 Get-AzResource 抛出错误。

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

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

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

  5. 我的本地计算机上没有问题。仅在Runbook中有问题。
always1988 回答:无法在Azure Runbook上运行Az命令

Runbook的环境与本地环境不同,如果要使用<div class="states-select row form-inline options col-md-12"> <label for="state_resident"><span> I am a resident of:</span> </label> <div class="form-group col-12 col-md-3 select-dropdown-wraper"> <div class="select-wrapper"> <select id="state_resident" name="state_resident" class="form-control require" autocomplete="off"> </select> </div> </div> </div> <div class="row form-group col-md-12 "> <div class="form-inline label request-heading"> <span> I am submitting this request: </span> </div> <div class="col-md-10"> <fieldset class="dark-font check_fields_ID"> <input class="fa fa-check check_one" type="checkbox" id="checked_me" value="myself-click" name="group2"/> <label for="checked_me"><span>For myself</span></label> <input class="fa fa-check check_two" type="checkbox" id="checked_someone" value="someone-else-click" name="group2"> <label for="checked_someone"><span>On behalf of someone else</span></label> </fieldset> </div> </div> <div class="row form-group col-md-12 "> <div class="form-inline label request-heading"> <span> I am a current or former customer: </span> </div> <fieldset class="dark-font check_fields_companies"> <input class="fa fa-check check_one" type="checkbox" id="checked_yes" value="myself-click" name="group2"/> <label for="checked_yes"><span>Yes</span></label> <input class="fa fa-check check_two" type="checkbox" id="checked_no" value="someone-else-click" name="group2"> <label for="checked_no"><span>No</span></label> </fieldset> </div> ,请按照以下步骤操作。

注意:请确保在创建如下所示的自动化帐户时创建了Get-AzResource

enter image description here

1。导航到门户网站中的自动化帐户-> Run As Account-> Modules->搜索Browse GalleryAz.Accounts-> Az.Resources其中。 Import的最新版本具有Az.Resources的依赖关系,如果您已经拥有Az.Accounts>=1.8.0的旧版本,只需将其删除并安装最新版本,然后安装{{1} }。

enter image description here

2。然后使用Powershell Runbook中的以下脚本。

Az.Accounts

enter image description here

,

好的,如果您想从PowerShell库中安装模块(如上所述),则需要安装NuGet软件包提供程序。我认为这失败了,因为您没有使用TLS 1.2。请在Install-PackageProvider行之前运行此行。

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

您应该确保正在运行提升的PowerShell提示符以执行模块的安装和卸载,只是提到这可能是您在这里遇到的问题。如果要在PowerShell 5.1中使用Az模块,则必须卸载AzureRm cmdlet。后一个模块是AzureRm模块的跨平台替换,现已弃用。如果证明Uninstall-AzureRM有问题,请在关闭所有打开的PowerShell控制台并以管理员身份运行卸载后,建议执行以下操作:

# You may want to check the prefix of the AzureRM modules first by just running Get-Module -ListAvailable
Get-Module -Name AzureRM* -ListAvailable | Remove-Module -Force

您可能需要对Program Files(x86)[PowerShell(x86)]和Program Files [PowerShell]都执行此操作,因为我认为这对两者都适用。

当然,如果您仅能使用PowerShell Core,我相信您可以在其中安装Az模块,而无需将其从PowerShell 5.1中删除。无论哪种方式,我都会按照Microsoft运行的方式安装完整的模块集合:

Install-Module -Name Az -AllowClobber #-Scope CurrentUser # Uncomment the scope if you want to just install it for yourself

以上所有内容都是一次性配置。您无需将其保留在任何脚本中,除非您要部署容器之类的东西,否则每次构建时都需要对其进行配置。

Get-AzResource依赖于Az.Resources。看来您没有该模块,并且您当前无法安装,可能是因为它无法先安装NuGet。如前所述,只要删除任何AzureRm模块,将SecurityProtocolType设置为Tls 1.2即可解决此问题。

本文链接:https://www.f2er.com/2317846.html

大家都在问