【问题标题】:The term 'Get-AzureRmPolicyDefinition' is not recognized as the name of a cmdlet, function, script file, or operable program术语“Get-AzureRmPolicyDefinition”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称
【发布时间】:2020-11-01 13:15:08
【问题描述】:

我对上一个任务有疑问,分配策略:

模块 AzureRM 包含 Resources 命名空间(带有 Get-AzureRmPolicyDefinition)应该已经包含,对吧? https://github.com/Azure/azure-powershell/blob/preview/src/ResourceManager/Resources/Commands.Resources/help/Get-AzureRmPolicyDefinition.md

那么为什么我会收到此错误消息?

“Get-AzureRmPolicyDefinition”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。

当我添加到 PowerShell 任务行时:

Install-PackageProvider -Name NuGet -Force -Scope CurrentUser

Install-Module -Name Az.Accounts -RequiredVersion 1.6.2 -Force -Scope CurrentUser -AllowClobber

Install-Module -Name AzureRM.Resources -RequiredVersion 6.7.3 -Force -Scope CurrentUser -AllowClobber

那我最后会得到:

程序集“Microsoft.Azure.Commands.ResourceManager.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”中类型“Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient”中的方法“get_SerializationSettings”确实如此没有实现。

如何处理这个问题?

2020-01-23T12:42:46.1108266Z ##[section]Starting: Assign Azure Policy
2020-01-23T12:42:46.1190930Z ==============================================================================
2020-01-23T12:42:46.1191016Z Task         : Azure PowerShell
2020-01-23T12:42:46.1191088Z Description  : Run a PowerShell script within an Azure environment
2020-01-23T12:42:46.1191160Z Version      : 4.159.7
2020-01-23T12:42:46.1191219Z Author       : Microsoft Corporation
2020-01-23T12:42:46.1191280Z Help         : [Learn more about this task](https://go.microsoft.com/fwlink/?LinkID=613749)
2020-01-23T12:42:46.1191368Z ==============================================================================
2020-01-23T12:42:48.5385840Z ##[command]Import-Module -Name C:\Modules\az_3.1.0\Az.Accounts\1.6.4\Az.Accounts.psd1 -Global
2020-01-23T12:42:50.3192937Z ##[warning]Both Az and AzureRM modules were detected on this machine. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found here: https://aka.ms/azps-migration-guide
2020-01-23T12:42:55.2184342Z ##[command]Clear-AzContext -Scope Process
2020-01-23T12:42:56.2685211Z ##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
2020-01-23T12:42:56.8285933Z ##[command]Connect-AzAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud
2020-01-23T12:42:57.9191448Z ##[command] Set-AzContext -SubscriptionId 78afced4-1c58-4e66-8242-c042890d34c3 -TenantId ***
2020-01-23T12:42:58.5652039Z ##[command]& 'D:\a\_temp\743dfd85-b908-48ee-9a00-e0ee97b44c8a.ps1' 
2020-01-23T12:42:59.9241324Z ##[command]Disconnect-AzAccount -Scope Process -ErrorAction Stop
2020-01-23T12:43:00.1484807Z ##[command]Clear-AzContext -Scope Process -ErrorAction Stop
2020-01-23T12:43:00.6509257Z ##[error]Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient' from assembly 'Microsoft.Azure.Commands.ResourceManager.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
2020-01-23T12:43:00.6856188Z ##[section]Finishing: Assign Azure Policy

【问题讨论】:

    标签: azure-devops azure-pipelines azure-pipelines-release-pipeline


    【解决方案1】:

    您的日志中的警告显示了导致的问题:

    在此计算机上检测到 Az 和 AzureRM 模块。阿兹和 AzureRM 模块不能在同一会话中导入或在 相同的脚本或运行手册。如果您正在运行 PowerShell 您控制的环境可以使用“Uninstall-AzureRm”cmdlet 从您的计算机中删除所有 AzureRm 模块....

    不确定您之前任务中的脚本是什么。但是基于此错误消息,我可以确定它是将AzureRm 模块安装到工作区中。因此,AzureRM 模块在后续步骤中与Az 模块发生冲突。

    Az 模块是我们开发并用于替换AzureRm 模块的新模块。但是当你将AzureRmAz模块安装到一个工作空间时,很容易导致conflict导致错误。


    您共享的脚本没有错误,因此只需在您的脚本之前运行Uninstall-AzureRm 以避免此 cmdlet 冲突。

    【讨论】:

    • 没错,这些模块不能同时安装。我还想分享我如何解决这个问题的信息:我找到了一个缺少命令的 relpacment(用于任务分配策略 v4)并使用`Get-AzPolicyDefinition(instead of Get-AzureRmPolicyDefinitionNew-AzPolicyAssignment -AssignIdentity -Location locationName 而不是New-AzureRmPolicyAssignment 来使其正常工作。
    • @Gajush,很高兴知道您找到了替代品!我看到这些替换命令都属于 Az 模块。感谢您在这里分享您用来解决问题的方法:-)
    • 伟大的工作......浏览了许多文件和网站......但没有使用......你简短的解释,比任何人解释得更多......谢谢......
    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 2022-07-30
    • 2021-11-01
    • 2021-05-23
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多