Wednesday 31 March 2021

Fetch User Details using Graph API

 

Param(
    $ApplicationID,
    $TenatDomainName,
    $AccessSecret
)

#Defining Application Details
$ApplicationID = "44b78d78-425a-446567-21527-a43kj39k2t0a8"
$TenatDomainName = "spscript.onmicrosoft.com"
$AccessSecret = "cba)b@Q~p~Ygk*%F~gcf27%170_g@#30~"

#Requesting Token
$Body = @{    
Grant_Type    = "client_credentials"
Scope         = "https://graph.microsoft.com/.default"
client_Id     = $ApplicationID
Client_Secret = $AccessSecret

$ConnectGraph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token" -Method POST -Body $Body

#Token Received and stored in Token Variable
$token = $ConnectGraph.access_token


#Fetching User Details 
$UsersURL = 'https://graph.microsoft.com/beta/Users/'
$Result = Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $UsersURL -Method Get

$Result.value|Select-Object userPrincipalName