Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Graph.Application version 2.0.0 AccessToken #2123

Closed
Ericvf opened this issue Jul 6, 2023 · 6 comments
Closed

Microsoft.Graph.Application version 2.0.0 AccessToken #2123

Ericvf opened this issue Jul 6, 2023 · 6 comments

Comments

@Ericvf
Copy link

Ericvf commented Jul 6, 2023

Describe the bug
Since version 2.0.0 of the Microsoft.Graph.Application powershell module we are not able to connect using an accesstoken.

To Reproduce

  Write-Host "##[debug] Installing module Microsoft.Graph.Applications"
  Install-Module -Name Microsoft.Graph.Applications -RequiredVersion 2.0.0 -Scope CurrentUser -Force 

  Write-Host "##[debug] Installing module MSAL.PS"
  Install-Module -Name MSAL.PS -Scope CurrentUser -Force
  
  Write-Host "##[debug] Get Msal Token"
  $MsalToken = Get-MsalToken -TenantId $($env:tenantId) -ClientId $($env:servicePrincipalId) -ClientSecret ($($env:servicePrincipalKey) | ConvertTo-SecureString -AsPlainText -Force)

  Write-Host "##[debug] Connecting to Microsoft Graph"
  Connect-MgGraph -AccessToken "$($MsalToken.AccessToken)"

Expected behavior
After runing this powershell script we expect to connect to Microsoft Graph. Instead we get an error saying:
Cannot bind parameter 'AccessToken'. Cannot convert the "****************" value of type "System.String" to type "System.Security.SecureString"

Module Version
2.0.0

Additional context
When we downgrade the module to version 1.28 it works again

@ghost ghost added the ToTriage label Jul 6, 2023
@ghost ghost added this to Issues to triage in Graph SDK - Triage Jul 6, 2023
@herromega
Copy link

fyi, 2.0.0 version also affects/breaks automation (app auth) via Get-WindowsAutoPilotInfo.
Modify of the script to require 1.28.0 makes it work again.

@Ericvf
Copy link
Author

Ericvf commented Jul 6, 2023

https://devblogs.microsoft.com/microsoft365dev/microsoft-graph-powershell-v2-is-now-in-public-preview-half-the-size-and-will-speed-up-your-automations/

We found out there is a new way to connect without using an AccessToken directly. This is the adapted script:

      Write-Host "##[debug] Installing module Microsoft.Graph.Applications"
      Install-Module -Name Microsoft.Graph.Applications -Scope CurrentUser -Force 

      Write-Host "##[debug] Connecting to Microsoft Graph"
      $clientSecret = ConvertTo-SecureString -String $($env:servicePrincipalKey) -AsPlainText -Force
      $ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $($env:servicePrincipalId), $clientSecret
      Connect-MgGraph -TenantId $($env:tenantId) -ClientSecretCredential $ClientSecretCredential

@Ericvf Ericvf closed this as completed Jul 6, 2023
Graph SDK - Triage automation moved this from Issues to triage to Closed Jul 6, 2023
@ghost ghost removed the ToTriage label Jul 6, 2023
@yllekz
Copy link

yllekz commented Jul 21, 2023

What is the actual fix for the error in the issue? A workaround isn't sufficient. The cmdlet still supports tokens and changed the type to securestring, so why does it still error out?

Edit: I got it working, you need to wrap the value of -AccessToken like so: ($MyToken | ConvertTo-SecureString -AsPlainText -Force)

@Ericvf
Copy link
Author

Ericvf commented Jul 21, 2023

I'd advice to use the ClientSecretCredential as suggested

@fajterini
Copy link

What is the actual fix for the error in the issue? A workaround isn't sufficient. The cmdlet still supports tokens and changed the type to securestring, so why does it still error out?

Edit: I got it working, you need to wrap the value of -AccessToken like so: ($MyToken | ConnectTo-SecureString -AsPlainText -Force)

Thanks, converting Access token to secure string solved my issue. :)
But please fix your typo ConnectTo-SecureString ->> ($MyToken | ConvertTo-SecureString -AsPlainText )

  • Also -Force parameter is default with powershel 7 so it's not needed.

@yllekz
Copy link

yllekz commented Aug 2, 2023

Thanks. Typo fixed. -Force is needed for 5.1 however, which is what my implementation uses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

4 participants