Connect-AzureAD not working with Powershell core

8.9k Views Asked by At

I am trying to run a powershell command - ConnectAzureAD and getting the below error-

'Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.7.16602, Culture=neutral,. Could not find or load a specific file.'

This was working earlier with Powershell 5 but not with powershell core.The versions that i am using are as: Powershell - 7.0.1 Az.Accounts - 1.8.1 (i have tried updating this but no luck) AzureAd - 2.0.2.104

Is there any workaroudn for this ? We tried Azure.Standard.Preview from 'Post test Gallery' but it failed the keyVault powershell commands. Any help on this?

3

There are 3 best solutions below

1
Joey Cai On

As Shiva said, this is a known limitation on .NET CORE that new version assembly cannot be loaded if old version is already loaded. PowerShell is considering module isolation but so far there is no good solution yet.

You can upgrade Microsoft.IdentityModel.Clients.ActiveDirectory to the latest version.

For more details, you could refer to this issue.

0
Joe Paul On

You could instead try to use the az rest invoking the graph api.

Until the time az-cli is in par with the AzureAD powershell module you can instead use the graph api

az login --tenant <tenantname.onmicrosoft.com>
$uri = "https://graph.microsoft.com/v1.0/applications"

$allApplications = az rest `
   --method GET `
   --uri $uri `
   --headers 'Content-Type=application/json' | convertfrom-json

$allApplications.value |% {"{0}-{1}" -f $_.appid, $_.displayname}

I have put some samples using az rest here, https://github.com/joepaulk/utilities/blob/master/manage-azuread-applicationregistrations.ps1

You may also refer to: https://damienbod.com/2020/06/22/using-azure-cli-to-create-azure-app-registrations/ from where i picked up inspiration

Other reference, how az rest use the accesstokens from az cli can be found here, https://mikhail.io/2019/07/how-azure-cli-manages-access-tokens/

1
Rahul Mahajan On
Install-Module -Name AzureADPreview -RequiredVersion 2.0.2.89 
Import-Module AzureADPreview -Version 2.0.2.89 -UseWindowsPowerShell