3

I have an azure function app with runtime version ~3 and powershell version 7.0 creating a connection to powerbi that is connecting to azure active directory.

Import-Module -Name MicrosoftPowerBIMgmt

$id=$user
$password=$token | ConvertTo-SecureString -AsPlainText -Force
$credential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $id, $password

Connect-PowerBIServiceAccount -Credential $credential

Throws an exception Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly

Above script works fine on powershell version 5.1 however not able to use old version on Azure Functions.

Tried to import with UseWindowsPowerShell Import-Module -Name MicrosoftPowerBIMgmt -UseWindowsPowerShell however it also throws an exception ERROR: Failed to generate proxies for remote module 'MicrosoftPowerBIMgmt'. Running the Get-Command command in a remote session returned no results.

Not able to understand why creating a connection to active directory is so hard and cumbersome, could anyone please help to resolve the above issue. Thanks in advance!

hagarwal
  • 1,153
  • 11
  • 27

1 Answers1

3

The fix is available at: https://www.powershellgallery.com/packages/MicrosoftPowerBIMgmt/1.2.0.

Please update your function app to use this module.


That class isn't supported by .NET Core and .NET 5, Powershell 7 is built on top of .NET Core and thus can't load the assembly. You'll need to stick to Windows Powershell 5.1 (which uses the .NET 4 runtime) or the MicrosoftPowerBIMgmt needs to be updated to not create the specific Sha256 implementation, see dotnet/runtime#25044 (comment).

enter image description here

There's a GitHub issue tracking the issue:

As a workaround, it looks like you should be able to auth using a certificate of a service principal:

MicrosoftPowerBIMgmt works well on desktop PowerShell 7.1.0 using service principal and auth by cert.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Oh, good to know it is a known issue. Since I am using email id & password, I does not have service principal and its thumbprint. – hagarwal Apr 23 '21 at 15:10
  • Looks like another workaround people are using is to invoke a PS5 session from PS7. A bit ugly,but I'm guessing they need to update a few dependencies. – jessehouwing Apr 23 '21 at 15:21
  • This is an issue that I have run into as well. Here's another thread with Microsoft (https://learn.microsoft.com/en-us/answers/questions/339901/34could-not-load-type-39systemsecuritycryptography.html?childToView=370002#comment-370002). Incidentally, I have submitted a ticket with my Azure team to investigate the issue as it is impacting implementation into production for me as well. I'll post here if I find a solution. – Matt_Davis Apr 26 '21 at 15:40