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

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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With