1
votes

I have problem with last task, assign policy:

enter image description here

Module AzureRM which contains Resources namespace (with Get-AzureRmPolicyDefinition) should be already included, right? https://github.com/Azure/azure-powershell/blob/preview/src/ResourceManager/Resources/Commands.Resources/help/Get-AzureRmPolicyDefinition.md

So why I have this error message?

The term 'Get-AzureRmPolicyDefinition' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

enter image description here

When I add to PowerShell task line:

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

Then I will at the end get:

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.

How to deal with that problem?

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
1

1 Answers

2
votes

The warning in your log has display the issue caused:

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....

Not sure what is your scripts in your previous tasks. But based on this error message, what I can sure is it installed the AzureRm module into the workspace. And as a result, AzureRM module has conflict with Az module in later steps.

Az module is the new module which we develope and used to replace the AzureRm module. But when you install the AzureRm and Az module into one workspace, it easily cause conflict which will lead the error.


There's no error on your scripts shared, so just need run Uninstall-AzureRm before your scripts to avoid this cmdlet conflicts.