Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Azure / Using Microsoft Graph API to Access Azure via PowerShell

November 30, 2021 AzureMicrosoft 365PowerShell

Using Microsoft Graph API to Access Azure via PowerShell

Microsoft Graph API allows you to access any objects in the Azure AD (Microsoft 365) tenant using a single REST API point (https://graph.microsoft.com). You are likely to think that it may be interesting to web developers only, but it is quite the other way round. Some data, objects, or resource properties in Microsoft 365 can only be accessed through Microsoft Graph. To collect analytic data, statistics, or other information, an Azure administrator has to use Microsoft Graph.

In this article, we’ll show you how to register your app in Azure AD, get an authentication token, connect to different Microsoft 365 resources (Azure AD, Office 365, Intune, SharePoint, Teams, OneNote, etc.) using RESTful and PowerShell Invoke-RestMethod cmdlet. You can use Microsoft Graph both to get data and manage objects in Azure.

Contents:
  • Registering Microsoft Graph Application on Azure AD
  • Connecting to Azure Microsoft Graph API Using PowerShell

Registering Microsoft Graph Application on Azure AD

To access resources in your Azure tenant using Microsoft Graph, you need to create a new Azure AD app and allow it to access different Azure objects.

  1. Sign-in to the Azure portal https://portal.azure.com/
  2. Go to Azure Active Directory -> App registration;
  3. Create a new app (New registration);
    register new app using azure app registration
  4. Enter the name of your app: azGraphPowerShellApp, select who can use the app: Accounts in this organizational directory only (tenantname only - Single tenant) and click Register;
    register application in Microsoft Azure
  5. Then select what Azure resources your application is allowed to access. Go to the API permissions section;
  6. By default, an app is allowed to read data about a current AzureAD user only (User.Read). We will grant it read permissions on all properties of Microsoft 365 users and groups;
  7. Click Add a permission, select Microsoft Graph;
  8. There are two basic permission types in Microsoft Graph (Delegated permission – when something is done on behalf of a user who runs an app and Application Permission – when an app is called by an external script). Select Application Permission;
  9. In the list that appears, you can select what permissions you will assign to your application to access Azure resources and objects. In my example, I have added Group -> Group.Read.All, GroupMember -> GroupMember.Read.All, User -> User.Read.All (if you want your app to read any data in your tenant, select Directory.Read.All);
    granting Azure resource permissions for the app
  10. Click Grant admin consent to grant access on behalf of the administrator.
    view assigned permissions for azure application

To authenticate in an app, you can use a certificate or a secret. A secret is an automatically generated password. The username is an app ID. Let’s create a secret for your app.

  1. Open Certificates & secrets -> New client secrets;
  2. Enter the key name and set its validity time(I have specified 12 months);
    create a secret for azure application
  3. Copy the value from the Value field (it is the password for the app). Save the password in the Azure Key Vault or in your password manager, since after you exit the app, the password value will be hidden (you will have to create the secret again);
    azure client secret
  4. Then copy your app ID (Application client ID) and Azure tenant ID (Directory tenant ID).

azure application credentials

Paste your values to PowerShell variables:

$ApplicationID = "1111111-1111-1111-1111-11111111111"
$TenatDomainName = "2222222-2222-2222-2222-222222222222"
$AccessSecret = "3333333333333333333333333333333333333333333"

Connecting to Azure Microsoft Graph API Using PowerShell

To use Microsoft Graph API from PowerShell, you don’t need to install any separate PowerShell modules (like Azure AD). You can interact with it using a built-in Invoke-RestMethod cmdlet.

To connect to Graph API, you must get an access token. The following PowerShell script allows you to authenticate in your app and get a Microsoft Graph API access token.

In this example we are using a secret (a password) as plain text in the script. In real life, it is not recommended to do it. You should better request a secret interactively or extract it from a secret vault (the SecretManagement PowerShell module can help you to do it). Also, take care of your secrets if you store your PowerShell scripts on Git.

$ApplicationID = "1111111-1111-1111-1111-11111111111"
$TenatDomainName = "2222222-2222-2222-2222-222222222222"
$AccessSecret = "3333333333333333333333333333333333333333333"
$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 = $ConnectGraph.access_token

powershell - getting Azure Graph token using Invoke-RestMethod

Using the token, you can run different queries against your Azure tenant using GraphAPI.

For example, the script below displays a list of groups in your Azure AD:

$GrapGroupUrl = 'https://graph.microsoft.com/v1.0/Groups/'
(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $GrapGroupUrl -Method Get).value.displayName

get azure groups via graph api

You can display the date when an Azure AD group was created:

$GrapGroupUrl = 'https://graph.microsoft.com/v1.0/Groups/'
$Groups=(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $GrapGroupUrl -Method Get).value
$Groups | select displayName,createdDateTime

To show a user name, UPN, and email address:

$GrapUserUrl = 'https://graph.microsoft.com/v1.0/users'
$users=(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $GrapUserUrl -Method Get).value
$users | select displayName,userprincipalname,mail

list Azure AD users via Microsoft Graph API

If you haven’t granted permissions to access Azure AD objects to your app, the following error will appear when trying to run Invoke-RestMethod:

The remote server returned an error: (403) Forbidden.

Invoke-RestMethod - The remote server returned an error: (403) Forbidden

In the examples above, we only read data from Azure AD using the GET method. But you can also use POST, PUT, PATCH, or DELETE methods to make changes. For instance, you can create a user in Azure AD, reset a password, change a description, etc.

To view available Microsoft Graph API properties or methods in your browser, you can use Graph Explorer (https://developer.microsoft.com/en-us/graph/graph-explorer).

exploring Azure AD Objects with MIcrosoft Graph Explorer

Microsoft also has a special Microsoft Graph PowerShell SDK for interacting with Microsoft Graph (Install-Module Microsoft.Graph). But we showed that you can access Microsoft Graph directly from PowerShell.

1 comment
1
Facebook Twitter Google + Pinterest
previous post
Get User or Group Creation Date in Azure AD (or MS365) with PowerShell
next post
How to Convert SID to User/Group Name and User to SID

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Removing Azure Arc Setup Feature on Windows Server...

November 9, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

1 comment

Alex Dryer July 25, 2023 - 9:21 pm

Hello! Thank you for this article, it made it easy to set up the API in Azure. You referenced this guide in a previous article about checking read/unread email status in Exchange here: https://woshub.com/check-read-unread-email-status-exchange/

I did some tinkering around in MS Graph after authenticating and was able to find the isRead information in one of the GET queries, but was only able to run it for the messages in my own inbox. I also wasn’t able to figure out how to specify which email ID I wanted to run the query on. Is this a limitation of the API or is there something I’m missing? I’d much appreciate a follow up to that article if its possible to use Graph for the same purpose as you outlined with the Get-MessageTrackingLog command in PowerShell.

Thank you!

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Checking User Sign-in Logs in Azure AD (Microsoft 365)
  • Whitelist Domains and Email Addresses on Exchange Server and Microsoft 365
  • How to Reset User Password in Azure Active Directory (Microsoft 365)
  • Enable or Disable MFA for Users in Azure/Microsoft 365
  • Enabling Modern or Basic Authentication for Microsoft 365
  • Manage Groups in Azure AD and Microsoft 365 Using PowerShell
  • How to Hide Users and Groups from the Global Address List on Exchange/Office 365?
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top