Search code examples
powershellazure-ad-msal

Get-MsalToken: The property 'Authority' cannot be found on this object


I am creating dashboard with Powershell Universal (https://docs.powershelluniversal.com/userinterfaces/dashboards) and I am trying to get an access token in order to connect to MS Graph. I am using the command Get-MsalToken (part of the MSAL.PS module) to get an access token. The code I am running is below:

$Params = @{
    ClientId = $Credential.UserName
    ClientSecret = $Credential.Password
    TenantId = 'xxxxxxxxxxxxxxxxxxxx'
    ForceRefresh = $true
    ErrorAction = 'Stop'
}
$AccessToken = (Get-MsalToken @Params).AccessToken

When running the above code on the command line using Powershell 7, it works fine. However when the dashboard runs the same command on Powershell 7, it returns this error message:

Get-MsalToken: The property 'Authority' cannot be found on this object. Verify that the property exists.

The module MSAL.PS is on version 4.37.00 on both my local instance and on the server where the dashboard is running from. For the most part I am unable to replicate the error on the command line on either my machine or on the server, and the command works perfectly. I was able to reproduce the error locally on my machine's command line after connecting to other services and then running Get-MsalToken, but trying this a second time is getting the access token correctly, so I am unable to accurately reproduce the error on the command line. The dashboard however is consistent with this error. Running an automated script from the server also does not produce the error.

I did see a forum on github about this same issue, but there is no clear resolution. I did see people recommending commenting out some lines from the source code, but given that they recommend commenting out a line for TenantID, which I am using for the connection, I am not in favor of doing that. The forum can be found here: https://github.com/AzureAD/MSAL.PS/issues/45

I was just wondering if anybody else has run into this issue or knows of a solution or alternative. Thanks!


Solution

  • As NiMux stated above, the solution is to simply import the MSAL.PS module before importing anything else. At the top of the main page of my code I simply added Import-Module -Name MSAL.PS -Force and I got it to work properly. Even though the module was already installed and imported, it needs to be imported before anything else.

    Hopefully this helps somebody else with this issue in the future.