Powerbi token is not generating with Az module Runbook.

I am updating the powerbi dataset from the Azure Automation account runbook, The code is working fine with AzureRm, but when I changed the code to Az module, then token generation code stop working. installed all the az modules in the Azure Automation account but still no luck.

error: GetAuthToken : Error in generating token: Cannot find an overload for “UserCredential” and the argument count: “2”. At line:473 char:16 + $authToken = GetAuthToken + ~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:slight_smile: [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,GetAuthToken

 

Code:

function GetAuthToken
{

try
{
#Add-Type -Path “C:\Program Files\WindowsPowerShell\Modules\Azure\5.1.2\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll”

$resourceAppIdURI = $url_resourceApp

$authority = $url_authority;

$AADcredential = New-Object -TypeName 'Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential'
-ArgumentList $AadAccountUserName,$AadAccountPassword

Create AuthenticationContext tied to Azure AD Tenant

$authContext = New-Object -TypeName 'Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext'
-ArgumentList $authority

$authResult = $authContext.AcquireTokenAsync($resourceAppIdURI,$ReportAppClientId,$AADcredential)

if ( ! $authResult -or ! $authResult.Result)
{
return $null;
}
$Token = $authResult.Result.CreateAuthorizationHeader();
Write-Host “Generated Auth Token Successfully”;
return $Token;

}
catch
{
$Error = $_.Exception.Message
Write-Error “Error in generating token: $Error”
return “Error”;
}

}

The error is telling you that you’re trying to provide 2 arguments on line 473 but there are no overloads that accept 2. What is on line 473?