How can i get a token from Microsodt.Graph using powershell?

55 Views Asked by At

I am trying to get a token from Microsoft.Graph using Powershell. I am using the following:

 $clientId = <clientid>
 $scopes = "https://graph.microsoft.com/.default"
 $tenantId = <tenantid>
 $token = Get-MsalToken -ClientId $clientId -TenantId $tenantId -Scopes $scopes
 $accessToken = $token.AccessToken

But when i run this i get an error :

 The term 'Get-MsalToken' is not recognized as the name of a cmdlet, function

Any ideas on how i can make this work or Get a token any other way ?

2

There are 2 best solutions below

1
Alexander Drogin On

I would assume you need to install the MSAL.PS module to use Get-MsalToken

Install-Module msal.ps
0
Toni On

this is what u need:

$clientId = #enter clientid here
$secret = #enter secret here
$tenantId = #enter tenantid here

$body = {
    Grant_Type = 'client_credentials'
    Scope = 'https://login.microsoftonline.com/default.'
    Client_Id = $clientId
    Client_Secret = $secret
}

$token = Invoke-RestMethod -Method Get -Body $body -uri "https//login.microsoft.com/$tenantId/oauth2/v2.0/token"

In regards to the error message u get:

The term 'Get-MsalToken' is not recognized as the name of a cmdlet, function

That indicates that the module can't be found. So specify the path to the module - e.g. import-module C:\mymodule\mymodule.ps1

But u don't need it anyways...