Skip to content

Connect-Graph : Authentication needed when call is within a function #124

Closed

Description

When I use the Graph Module inline, everything works as expected, for example:

Connect-Graph -ClientId $ClientId -TenantId $TenantId -CertificateThumbprint $CertificateThumbprint 
Get-MgUser -Top 1

This provides the expected Output.

Although when I use Connect-Graph within a Function, it fails:

function test-graph {
    $ClientId = "<omitted>"
    $TenantId ="<omitted>"
    $CertificateThumbprint ="<omitted>"
    
    Connect-Graph -ClientId $ClientId -TenantId $TenantId -CertificateThumbprint $CertificateThumbprint -ForceRefresh
    Get-MgUser -Top 1
}

test-graph

This fails with the Message:

Welcome To Microsoft Graph!
Get-MgUser_List: 
Line |
   8 |      Get-MgUser -Top 1
     |      ~~~~~~~~~~~~~~~~~
     | Authentication needed, call Connect-Graph.

I can reproduce this Error in PS 5.1 and in PS7. I also tried different ways of installing and importing the Graph Module (Global), without improvement.

Activity

  1. added this to the 0.5.0 milestone on Mar 26, 2020
  2. bodangly commented on Apr 4, 2020

    @bodangly

    I can add to this and say I have the same behavior.

    Some things to note. If I don't give it a -TenantId, I will get the prompt to do a devicelogin, even if called within a function. However, this context isn't persisted and all further mg functions fail. I have to login from an interactive prompt as stated for the context to be preserved.

    If I do specify a -TenantId, I don't even get the devicelogin prompt, it just outputs "Welcome to Microsoft Graph!" (if I pipe to Write-Host in the script), and no context actually gets captured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions