Search code examples
azurepowershellpowershell-az-module

Connect-AzAccount Could not load type 'System.Diagnostics.Metrics.Meter' from assembly 'System.Diagnostics.DiagnosticSource


Everything that I have found with a similar error seems to be for developers working in their C# / Visual Studio environment. I'm just trying to connect to Azure with Powershell. This seems to be a problem only with Powershell versions that are newer than 5.1.17763.5576. I have other VM's where things work fine and are practically identical, but the Powershell version is 5.1.17763.5576 or older. So, to state it plainly, my credentials are not the problem, and updating PowerShell to the latest is not the solution.

PS C:\Windows\system32> $psversiontable

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


PS C:\Windows\system32> connect-azaccount
Please select the account you want to login with.

WARNING: Unable to acquire token for tenant 'organizations' with error 'InteractiveBrowserCredential authentication
failed: Could not load type 'System.Diagnostics.Metrics.Meter' from assembly 'System.Diagnostics.DiagnosticSource,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.'
WARNING: Please run 'Connect-AzAccount -DeviceCode' if browser is not supported in this session.
connect-azaccount : InteractiveBrowserCredential authentication failed: Could not load type
'System.Diagnostics.Metrics.Meter' from assembly 'System.Diagnostics.DiagnosticSource, Version=5.0.0.0,
Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
At line:1 char:1
+ connect-azaccount
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Connect-AzAccount], AuthenticationFailedException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand

How do I fix this?

Things I've tried that do not work:

Update-Module -Name Az -Force  (does not fix this)
Uninstall/Reinstall Module     (does not fix this)

After playing with this more, I found that the problem is specifically AFTER I load my .psm1 PS module and instantiate it: ( It only happens after I [MyClass]::new() )


Solution

  • The solution was that I needed to:

    Import-Module Az.Accounts
    

    before loading my modules, or within my class definition itself.

    class MyClass {
        [string]$azureTenantId
    
        # Constructor
        MyClass () {
    
        }
    
        [void] connectAzAccount(){
            #this is required        
            Import-Module Az.Accounts
            Connect-AzAccount -TenantId $this.azureTenantId
        }
    
        [void] setTenantId([string]$tenantId){
            $this.azureTenantId = $tenantId
        }
    }