azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Not able to connect to Azure with Az.Accounts version 2.12.3 (get_SerializationSettings) from within Visual studio code / Azure Automation runbook on Hybrid Worker

Open mrvdsah opened this issue 1 year ago • 75 comments

Description

With the latest version of Az.Accounts version 2.12.3 we face an issue when running the Connect-AzAccount command in PowerShell 5.1.3 terminal in Visual Studio code. The previous version(s) 2.12.2 did not throw an error.

The error occurs with fully installed AZ module latest version 10.0.0.0 or only having the Az.Accounts module installed.

Issue script & Debug output

Connect-AzAccount : Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient' from assembly 'Microsoft.Azure.PowerShell.Clients.Reso
urceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
At line:1 char:1
+ Connect-AzAccount
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Connect-AzAccount], TypeLoadException
    + FullyQualifiedErrorId : System.TypeLoadException,Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand

Environment data

Name                           Value
----                           -----
PSVersion                      5.1.17763.3770
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.3770
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module versions

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.12.3     Az.Accounts                         {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault...}

Error output

Resolve-AzError : Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient' from assembly 'Microsoft.Azure.PowerShell.Clients.ResourceManager, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
At line:1 char:1
+ Resolve-AzError
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Resolve-AzError], TypeLoadException
    + FullyQualifiedErrorId : System.TypeLoadException,Microsoft.Azure.Commands.Profile.Errors.ResolveError

mrvdsah avatar May 31 '23 09:05 mrvdsah

Can confirm. Have the same problem.

BartDecker avatar May 31 '23 11:05 BartDecker

Same here

Poolshark314 avatar Jun 02 '23 16:06 Poolshark314

Ditto...

flizzer avatar Jun 02 '23 22:06 flizzer

Same here. It fails i Terminal as well Name Value


PSVersion 5.1.22621.963 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.22621.963 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1

jdarre avatar Jun 03 '23 19:06 jdarre

Hi thanks for reporting. We are aware of this compatibility issue and are working with PowerShell to get it fixed. Let me summarize the problem here:

Symptom

The Windows PowerShell 5.1 terminal provided by the PowerShell VS Code extension cannot import Az.Resources v6.6.1. The error you receive is: "Register-AzModule : The type initializer for 'Microsoft.Azure.Commands.Common.AzModule' threw an exception".

Root cause

The Windows PowerShell 5.1 runtime preloads the PowerShellEditorServices module, which depends on Newtonsoft.Json 11.0. This issue is similar to one that caused a conflict with Az.Accounts.

Workarounds

  • Use PowerShell 7 instead of Windows PowerShell 5.1.
    • Install PowerShell 7
    • Go to the command palette of VS Code (F1 or Ctrl + Shift + P), select: "PowerShell: Show Session Menu" and then select: "Switch to: PowerShell (x64)"
  • Downgrade Az modules (Az.Accounts v2.12.1, Az.Resources v6.6.0)
  • Use the Windows PowerShell console or ISE installed on your system

Solution

We have contacted the team who maintains the PowerShellEditorServices module for an update of the library. According to them, it is inherited from the OmniSharp library that provides our LSP layer. We are in the process of following up with Omnisharp, which is part of .NET foundation.

isra-fel avatar Jun 04 '23 04:06 isra-fel

Workarounds

  • Use PowerShell 7 instead of Windows PowerShell 5.1.

    • Install PowerShell 7
    • Go to the command palette of VS Code (F1 or Ctrl + Shift + P), select: "PowerShell: Show Session Menu" and then select: "Switch to: PowerShell (x64)"

Is it possible that it doesn't work in PS7 ?

Name Value
PSVersion 7.3.4
PSEdition Core
GitCommitId 7.3.4
OS Microsoft Windows 10.0.22621
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

I had to downgrade the two mentioned Az modules (Az.Accounts v2.12.1, Az.Resources v6.6.0) to make it work.

axdko avatar Jun 05 '23 08:06 axdko

Can't wait to upgrade to PowerShell 7.0 to leave the nightmare of the newtonsoft dll. See as well https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/resolving-dependency-conflicts?view=powershell-7.3.

However Azure Automation only support 5.1 for production. PowerShell > 7.1 is already for I think over 1 year in preview (so we cannot switch to core because of it being in preview still).

As well as some M365 power shell libraries are not compatible yet with PowerShell core.

mrvdsah avatar Jun 05 '23 10:06 mrvdsah

Not a VSCode only issue: Also happens with Azure Automation runbook on Hybrid Worker (WS2K22), latest Az Module installed.

ElSrJuez avatar Jun 14 '23 06:06 ElSrJuez

Not a VSCode only issue: Also happens with Azure Automation runbook on Hybrid Worker (WS2K22), latest Az Module installed.

Note that because of the retiring run as accounts this should be the replacement. Downgrading to a earlier version does not seem a very stable mitigation. I also noticed that a brand new Automation account is leveraged with a lower version "out of the box" which is not returned after getting the module through the gallery.

rbeltman avatar Jun 14 '23 13:06 rbeltman

I am also facing the same issue using Automation Account runbooks with PS version 5.

alvindutt avatar Jun 15 '23 04:06 alvindutt

I did the install of Powershell 7 and also Downgrade Az modules but now I'm facing a different issue. Somebody could help me please?

Bug running a powershell script as Administrator on Powershell 7 Connect-AzAccount: C:\Nebula\Configuration\Scripts\Meridian\ImportSSLCertificate.ps1:21 Line | 21 | Connect-AzAccount -ServicePrincipal -Tenant $BootstrapConfig.AzureTen … | ~~~~~~~~~~~~~~~~~ | The 'Connect-AzAccount' command was found in the module 'Az.Accounts', but the module could not be loaded due to | the following error: [Assembly with same name is already loaded] For more information, run 'Import-Module | Az.Accounts'.

Import-Module PS C:\Nebula\Configuration\Scripts> Import-Module Az.Accounts Import-Module: Assembly with same name is already loaded

Modules

PS C:\Nebula\Configuration\Scripts> Get-Module -listavailable
    Directory: C:\Users\default.vm\Documents\PowerShell\Modules
ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     2.12.1                Az.Accounts                         Core,Desk {Disable-AzDataCollection, Disable-AzCo…
Script     5.7.1                 Az.Compute                          Core,Desk {Remove-AzAvailabilitySet, Get-AzAvaila…
Script     4.9.2                 Az.KeyVault                         Core,Desk {Add-AzKeyVaultCertificate, Update-AzKe…
Script     6.6.0                 Az.Resources                        Core,Desk {Get-AzProviderOperation, Remove-AzRole…
Script     4.6.0                 Az.Sql                              Core,Desk {Invoke-AzSqlServerExternalGovernanceSt…
Script     5.6.0                 Az.Storage                          Core,Desk {Get-AzStorageAccount, Get-AzStorageAcc…
    Directory: C:\program files\powershell\7\Modules
ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Manifest   7.0.0.0               CimCmdlets                          Core      {Get-CimAssociatedInstance, Get-CimClas…
Manifest   1.2.5                 Microsoft.PowerShell.Archive        Desk      {Compress-Archive, Expand-Archive}
Manifest   7.0.0.0               Microsoft.PowerShell.Diagnostics    Core      {Get-WinEvent, New-WinEvent, Get-Counte…
Manifest   7.0.0.0               Microsoft.PowerShell.Host           Core      {Start-Transcript, Stop-Transcript}
Manifest   7.0.0.0               Microsoft.PowerShell.Management     Core      {Add-Content, Clear-Content, Get-Clipbo…
Manifest   7.0.0.0               Microsoft.PowerShell.Security       Core      {Get-Acl, Set-Acl, Get-PfxCertificate, …
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        Core      {Export-Alias, Get-Alias, Import-Alias,…
Manifest   7.0.0.0               Microsoft.WSMan.Management          Core      {Disable-WSManCredSSP, Enable-WSManCred…
Script     1.4.8.1               PackageManagement                   Desk      {Find-Package, Get-Package, Get-Package…
Script     2.2.5                 PowerShellGet                       Desk      {Find-Command, Find-DSCResource, Find-M…
Script     7.0.0.0               PSDiagnostics                       Core      {Disable-PSTrace, Disable-PSWSManCombin…
Script     2.2.6                 PSReadLine                          Desk      {Get-PSReadLineKeyHandler, Set-PSReadLi…
Binary     2.0.3                 ThreadJob                           Desk      Start-ThreadJob
    Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     1.0.1                 Microsoft.PowerShell.Operation.Val… Desk      {Get-OperationValidation, Invoke-Operat…
Binary     1.0.0.1               PackageManagement                   Desk      {Find-Package, Get-Package, Get-Package…
Script     3.4.0                 Pester                              Desk      {Describe, Context, It, Should…}
Script     1.0.0.1               PowerShellGet                       Desk      {Install-Module, Find-Module, Save-Modu…
Script     1.2                   PSReadline                          Desk      {Get-PSReadlineKeyHandler, Set-PSReadli…
    Directory: C:\Program Files\Microsoft Monitoring Agent\Agent\PowerShell
ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Binary     1.0.0.0               Microsoft.MonitoringAgent.PowerShe… Desk
    Directory: C:\Program Files\Microsoft Monitoring Agent\Agent\AzureAutomation\7.3.1722.0
ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Binary     1.0                   HybridRegistration                  Desk
    Directory: C:\Program Files\Microsoft Monitoring Agent\Agent\AzureAutomation\7.3.1722.0\HybridAgent\Modules
ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Binary     1.0                   Orchestrator.AssetManagement.Cmdle… Desk      {Get-AutomationVariable, Set-Automation…

danilomsousa avatar Jun 22 '23 16:06 danilomsousa

Just another +1 here, having the same issue with AutomationAccount Runbooks running on a hybrid worker VM. Seems there's some issues with PS Core in runbooks and which are still in preview so I'm limited to running 5.1.

My only option is to use 5.1 and therefore downgrade the module, but this also causes the keyvault module to fail due to version incompatibilities so I've had to downgrade that 2x also.

insomniacc avatar Jun 28 '23 17:06 insomniacc

What is the latest status of this issue?

mrvdsah avatar Jun 29 '23 08:06 mrvdsah

+1 same issue here :(

Install-Module -Name MSOnline
Install-Module -Name Az.Resources
Install-Module -Name Az.Accounts

connect-msolservice 
Connect-AzAccount

PS C:\Windows\system32> Connect-AzAccount
Connect-AzAccount : Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient' from assembly
'Microsoft.Azure.PowerShell.Clients.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
At line:1 char:1
+ Connect-AzAccount
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Connect-AzAccount], TypeLoadException
    + FullyQualifiedErrorId : System.TypeLoadException,Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand

marcoflorian avatar Jul 03 '23 03:07 marcoflorian

Hi,

Facing the same issue. Spent several hours to try to have a stable dev env with Az PS in VS Code. Any update about a fix?

pmoisontech avatar Jul 06 '23 17:07 pmoisontech

Not a VSCode only issue: Also happens with Azure Automation runbook on Hybrid Worker (WS2K22), latest Az Module installed.

We've seen this issue without using Hybrid Workers; pure Azure Automation with Managed Identity (no RunAs usage).

StevieLamb avatar Jul 09 '23 11:07 StevieLamb

Using Windows PowerShell is not a workaround. I and a team member have reliably reproduced this issue via that mechanism.

Downgrading to Az.Accounts 2.12.1 and Az.Resources 6.6.0 also fails to resolve.

This issue gives more details on the scenarios in which I've reproduced the issue: https://github.com/Azure/azure-powershell/issues/9728#issuecomment-1625902173

Everything works in v7, but that's just not a solution when scripting for two major MSFT platforms: Endpoint Manager MDM scripts, and Azure Automation. That runtime cannot be targeted by Intune, and is not in production in AA.

StevieLamb avatar Jul 10 '23 11:07 StevieLamb

Hi, I'm glad to announce the issue was resolved, please see this comment for more information.

For maintenance purpose I'm closing this issue. If you need us to follow up on anything please reply in https://github.com/Azure/azure-powershell/issues/21647 . Thanks.

isra-fel avatar Jul 12 '23 17:07 isra-fel

I am glad you solved the Azure automation issue but I am not sure why you closed this issue. This issue seems to be mostly based around the issue that was not fixed.

“And for the conflict with the vscode terminal provided by the PowerShell extension, unfortunately the issue is still there. We couldn't get response from the maintainer of OmniSharp/csharp-language-server-protocol , who we hope can update their dependency of Newtonsoft.Json hence resolve the conflict. I'll update the thread once we get any updates.”

mark3grahams avatar Jul 13 '23 00:07 mark3grahams

This issue seems to be mostly based around the issue that was not fixed.

Hey @mark3grahams , yes, this issue was about vscode originally. But the conversation quickly got mixed by the other issue with Azure Automation. I closed this one purely to avoid confusion and duplication.

isra-fel avatar Jul 13 '23 10:07 isra-fel

Also getting this issue Az.Accounts version 2.12.5, PS 5.1 windows 10 (VSCode)

KeysAU avatar Aug 02 '23 07:08 KeysAU

This issue seems to be mostly based around the issue that was not fixed.

Hey @mark3grahams , yes, this issue was about vscode originally. But the conversation quickly got mixed by the other issue with Azure Automation. I closed this one purely to avoid confusion and duplication.

#21647 is specific to working with Azure Automation. This ticket was originally opened specific to the need for support within VSC. @isra-fel - please re-open this ticket so that it can be used to track this issue through to completion only after this is appropriately again working within VS Code.

ziesemer avatar Aug 07 '23 16:08 ziesemer

Same problem here. :( had to downgrade @isra-fel please reopen issue, because it is not azure automation related. it is vscode related.

superfliege avatar Aug 11 '23 12:08 superfliege

Any update on an actually resolution of this issue? It's getting really annoying juggling multiple versions.

SkitariiAuxiliary avatar Aug 24 '23 05:08 SkitariiAuxiliary

What worked for me, was to downgrade Az.Accounts module to 2.12.1 Get currently installed version: Get-Module Az.Accounts -ListAvailable Uninstall it: Uninstall-Module Az.Accounts -RequiredVersion <your-version> Install downgraded one: Install-Module Az.Accounts -RequiredVersion 2.12.1

yevheniilavrenchuk avatar Aug 29 '23 08:08 yevheniilavrenchuk

I got the same exception when trying to fetch secrets from a key vault using Get-AzureKeyVaultSecret.
As I didn't want to use a specific, older, version of Az.Accounts, I investigated further and found that Enable-AzureRmAlias solved the issue for me:

Remove-Module AzureRM.Profile -Force -ErrorAction SilentlyContinue  # AzureRM causes a conflict with Az modules

if (!(Get-Module -ListAvailable -Name Az.Accounts)) {
  Install-Module -Name Az.Accounts -Repository PSGallery -AllowClobber -Force -Scope CurrentUser  
} 
if (!(Get-Module -ListAvailable -Name Az.KeyVault)) {
  Install-Module -Name Az.KeyVault -Repository PSGallery -AllowClobber -Force -Scope CurrentUser 
} 

Import-Module Az.Accounts
Import-Module Az.KeyVault

Enable-AzureRmAlias -Scope CurrentUser -ErrorAction SilentlyContinue  # solves type implementation exception

Reinhard-S avatar Sep 15 '23 11:09 Reinhard-S

Those with a custom profile should beware the command

Enable-AzureRmAlias -Scope CurrentUser -ErrorAction SilentlyContinue # solves type implementation exception

Attempting this a year ago had no impact on the problem, but did break my profile. So your mileage will vary

StevieLamb avatar Sep 15 '23 11:09 StevieLamb

I have a brand new set up and I'm getting these exact same problems as OP. Any updates on this major issue?

Ramadel avatar Sep 28 '23 14:09 Ramadel

Same problem here.

btfromcc avatar Oct 05 '23 12:10 btfromcc

Same issue here with VSCode and Automation Account.

Fix is to:

Uninstall-Module Az.Accounts -RequiredVersion 2.13.1 Install-Module Az.Accounts -RequiredVersion 2.12.1 -Force

and Az.Resources to 6.6.0

EDIT! does not fix it, now I can't run 'Get-AzRouteTable' as it requires 'Az.Accounts' Version 2.13.0 and above. What a headache.

System.Management.Automation.CommandNotFoundException: The 'Get-AzRouteTable' command was found in the module 'Az.Network', but the module could not be loaded. For more information, run 'Import-Module Az.Network'.

This module requires Az.Accounts version 2.13.0. An earlier version of Az.Accounts is imported in the current PowerShell session. Please open a new session before importing this module. This error could indicate that multiple incompatible versions of the Azure PowerShell cmdlets are installed on your system. Please see https://aka.ms/azps-version-error for troubleshooting information. The module to process 'Az.Network.psm1', listed in field 'ModuleToProcess/RootModule' of module manifest 'C:\Program Files\WindowsPowerShell\Modules\Az.Network\6.2.0\Az.Network.psd1' was not processed because no valid module was found in any module directory.

mundayn avatar Oct 06 '23 00:10 mundayn