Search code examples
powershellmicrosoft-graph-apiazure-powershell

Has PowerShell 7.2.12 broken Connect-MgGraph with AccessToken?


Since upgrading our build agents from PowerShell 7.2.11 to 7.2.12, they have been reporting the following error:

Cannot bind parameter 'AccessToken'. Cannot convert the *** value of type "System.String" to type "System.Security.SecureString".

Our script is as follows:

param(
    [Parameter(Mandatory)]
    [string]$graphApiToken
)

Connect-MgGraph -AccessToken $graphApiToken

This was working previously, and rolling back to our previous build agent image has resolved the issue.


Solution

  • As mentioned in the comments, this is a change in behavior between v1.0 and v2.0 of the Microsoft Graph PowerShell module.

    If you want your scripts to maintain compatibility with v1.0, simply convert the access token value conditionally:

    param(
        [Parameter(Mandatory)]
        [string]$graphApiToken
    )
    
    $targetParameter = (Get-Command Connect-MgGraph).Parameters['AccessToken']
    
    if ($targetParameter.ParameterType -eq [securestring]){
      Connect-MgGraph -AccessToken ($graphApiToken |ConvertTo-SecureString -AsPlainText -Force)
    }
    else {
      Connect-MgGraph -AccessToken $graphApiToken
    }