Skip to content

Adding the ability to surface the access token used by the connection #2023

Closed

Description

Discussed in #2010

Originally posted by glennvanrymenant May 15, 2023
Would it be possible to add the ability to surface the access token used by the connection (Connect-MgGraph).

I prefer to call the Graph API directly (using Invoke-MgGraphRequest rather than the specific cmdlets) but ideally I would like call the Graph API directly with Invoke-WebRequest using the access token of the Graph SDK connection.

Read: I want to piggy-back on the auth part of the module (mainly when leveraging delegated permissions as using the auth of the module is easier than writing and maintaining your own authorization code flow functions).

Activity

  1. mortenlerudjordet commented on Jul 19, 2023

    @mortenlerudjordet

    Would also like to see the MS.Graph powershell module follow the same patterns as the other first party Microsoft Azure modules.
    As Merill Fernando (@merill) has already pointed out.

  2. glennvanrymenant commented on Nov 23, 2023

    @glennvanrymenant

    What is the status of this request? Looks orphaned 🤔.

  3. JankeSkanke commented on Apr 9, 2024

    @JankeSkanke

    Hoping someone can progress on this. There are many tools out there in the community currently stuck with using MSAL.PS because its to hard to move away from it. Please get some work started to build the Get-MgAccessToken cmdlet.
    Darrel (@darrelmiller) Petr Hollay (@petrhollayms)

  4. davidobrien1985 commented on Apr 27, 2024

    @davidobrien1985

    I don't understand why we (the customers and community) have to go through these discussions with every single team that manages a CLI or PowerShell module.

    As Merill Fernando (@merill) mentioned, many other Microsoft tools already support this. Darrel (@darrelmiller) please consider exposing this capability like so many other tools already do.

    Thank you!

  5. DanielBradley1 commented on Apr 27, 2024

    @DanielBradley1
    Contributor

    I would also like to advocate for this change, as it would drastically help improve some of my pipelines. Currently, https://github.com/SCOMnewbie/PSMSALNet works quite well, but I would prefer to use a supported, MS-backed, module :)

  6. davidobrien1985 commented on Apr 30, 2024

    @davidobrien1985

    Someone on Twitter pointed me to this here:

    $data = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/me" -Method GET -OutputType HttpResponseMessage
    $data.RequestMessage.Headers.Authorization.Parameter
    

    This indeed works.

  7. noopman commented on May 3, 2024

    @noopman

    Would that not only get the token with the claims for what you asked for? If I want to do something else, I'd need a different token.

    Either way, I second (fifth, sixth whatever) this request. I am in the exact same situation now. I can auth with Azure and then use Get-AzToken, but "it's not the right context for that". I don't want to go to the Azure context to get the token for my Entra context.

    How about this method. Ask Entra for a token by making a POST to:

    [Uri]"https://login.microsoftonline.com/$(Get-TenantId)/oauth2/token"

    My challenge becomes that we need a "resource" in the body. This works well when asking for a token to, for example, call an API.

  8. glennvanrymenant commented on Jul 30, 2024

    @glennvanrymenant

    Petr Hollay (@petrhollayms), Merill Fernando (@merill) since this is still open and hasn't been closed, is there any progress you can share? Can we expect the module to support generating/exposing tokens, or do we continue to rely on workarounds to grab the token?

  9. JeremyTBradshaw commented on Oct 24, 2024

    @JeremyTBradshaw

    Darrel (@darrelmiller) Petr Hollay (@petrhollayms) Merill Fernando (@merill) glennvanrymenant Carol Kigoonya (@CarolKigoonya) Magnus Mårtensson (@noopman) Jan Ketil Skanke [MVP] (@JankeSkanke) trying not to miss anyone who's been asking for this...
    https://learn.microsoft.com/en-us/entra/msal/dotnet/advanced/powershell-support - this page seems to state it outright. MS Graph PowerShell SDK is the official PowerShell thing that wraps in MSAL.NET. If we want to do things like SASL XOAuth2 for SMTP/IMAP/POP, we could use MailKit as officially recommended but then we need a way to obtain our own access token. We can do all this using C# and other programming languages that have been blessed by MSAL.NET, but in PowerShell we have to go unsupported to get our hands on an access token. This module is the correct place for this to finally happen.

    I struggle to understand why the hesitation for just PowerShell alone. It's not a matter of difficulty, that excuse can't work. We can easily get it done already in PowerShell, and it's super easy in C#, plus we can do C# things in PowerShell, and there are other easy libraries for MSAL.NET. Can't justify this any further. It is justified and please acknowledge and do something about this.

  10. merill commented on Oct 26, 2024

    @merill
    Contributor

    That's a good point you raise JeremyTBradshaw.

    Is this PowerShell module a Graph SDK that aligns in spirit to the rest of the Graph SDKs like C#, JS, etc that is intended for developers or is this module's primary audience non-devs?

    If it's an SDK, why not provide complete access to the underlying MSAL library that the other Graph libraries like C# and JS do.

    Darrel (@darrelmiller) I believe in the past you were leaning towards this module being an SDK and the users of this module being devs → #513 (comment)

  11. JeremyTBradshaw commented on Nov 13, 2024

    @JeremyTBradshaw

    Darrel (@darrelmiller) It seems like a perfect point has been made. You stated that PowerShell users are developers and should not get special treatment vs other programming language developers. But now here in this topic, PowerShell developers are being treated differently. Can we please have an update on this topic?

  12. Mike-Crowley commented on Oct 19, 2026

    @Mike-Crowley

    This came back last month as #3806 (Get-MgAccessToken returning a SecureString), prompted by the assembly isolation in 2.41.0 (#3789): the unofficial [Microsoft.Graph.PowerShell.Authentication.GraphSession]::Instance route no longer works on PowerShell 7, because that type now lives in a private AssemblyLoadContext. The Invoke-MgGraphRequest -OutputType HttpResponseMessage header trick posted above still works on 2.41.2 (checked with a fabricated token, so no sign-in involved), but it leaves a plain string in a variable, which is the main thing a SecureString-returning cmdlet would fix. Linking the two so the history here, including the 2023 concerns about token reuse, is visible there.

    🤖 Claude Mythos 5.1 (max effort) via Claude Code

  13. earbona23 commented on Nov 5, 2026

    @earbona23

    This looks like it was closed by accident a few hours ago, and I think it needs reopening.

    #3833 merged at 00:09:09Z today and closed this issue and #3806 as completed. #3895 then reverted #3833 in full at 01:48:31Z, 99 minutes later, to restore the Authentication assembly isolation. Get-MgAccessToken is not in main: searching the tree for the cmdlet name returns no match in source.

    So the feature is not shipped, but the two issues asking for it read as resolved. Anyone subscribed here since 2023 now believes this landed.

    Nothing to do with the work itself — the revert reason is sound and Ramses Sanchez-Hernandez (@ramsessanchez) already wrote the design for the follow-up in the revert body. This is only about the issue state.

  14. added a commit that references this issue on Nov 5, 2026
    5dd9caa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions