How to Automate Intune report with PowerShell

This article will help you automation Intune report with the PowerShell.

Benefits-

  • You don’t required to login in Intune console, PowerShell will do it for you.
  • You don’t required to enter Username and Password to access the Intune data.
  • It will be a Passwordless script, you can schedule this report.

Configuration Required- 

You have to create a App registration in Azure AD. Follow my article to create App Registration How to create Intune App Registration. 

Once you done with the App registration then collect ApplicationID, TenantID and Secret Code from you app Registration the use the below PowerShell code.

Download the script from my Github profile – Intune Device List.ps1

Below script will pull all devices data from the Intune.

# Script Name – Get All Device list from Intune
# Script Owner – Harvansh Singh
# Required details – This is a passwordless authentication script that will help you to full data from Intune.
# Reuired field – $TenantID, $AppID, $AppSecret

# To get the Token key

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$TenantID = “”
$AppID = “”
$AppSecret = “”

$ResourceAppIDuri = “https://graph.microsoft.com”
$oauthUri = “https://login.microsoftonline.com/$TenantID/oauth2/token”
$body = [Ordered] @{
resource = “$ResourceAppIDuri”
client_id = “$AppID”
client_secret = “$AppSecret”
grant_type = ‘client_credentials’
}
$response = Invoke-RestMethod -Method Post -Uri $oauthUri -Body $body -ErrorAction Stop
$aadToken = $response.access_token

$headers = @{
‘Content-Type’ = ‘application/json’
Accept = ‘application/json’
Authorization = “Bearer $aadToken”
}

$uri = “https://graph.microsoft.com/beta/deviceManagement/managedDevices”
$Devicelist = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers
$devicedata = $Devicelist.value | Select-Object deviceName, joinType, deviceEnrollmentType,operatingSystem, complianceState, enrolledDateTime, lastSyncDateTime
$devicedata | Export-Csv “D:\Intune Automation\All Devices.csv” -NoTypeInformation

 You device output will save on D:\Intune Automation\All Devices.csv Drive. 

Below script will pull All Operating System count data from the Intune.

Download this script from my Gitbub profile Get Device Compliance Count.ps1

# Script Name – Get Device Complaince Count
# Script Owner – Harvansh Singh
# Required details – This is a passwordless authentication script that will help you to full data from Intune.
# Reuired field – $TenantID, $AppID, $AppSecret
# To get the Token key


[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$TenantID = “”
$AppID = “”
$AppSecret = “”

$ResourceAppIDuri = “https://graph.microsoft.com”
$oauthUri = “https://login.microsoftonline.com/$TenantID/oauth2/token”
$body = [Ordered] @{
resource = “$ResourceAppIDuri”
client_id = “$AppID”
client_secret = “$AppSecret”
grant_type = ‘client_credentials’
}
$response = Invoke-RestMethod -Method Post -Uri $oauthUri -Body $body -ErrorAction Stop
$aadToken = $response.access_token

$headers = @{
‘Content-Type’ = ‘application/json’
Accept = ‘application/json’
Authorization = “Bearer $aadToken”
}
$uri = “https://graph.microsoft.com/beta/deviceManagement/managedDeviceOverview”
$OSlist = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers
$OSListdata = $OSlist.deviceOperatingSystemSummary | Export-Csv “D:\Intune Automation\OSList.csv” -NoTypeInformation

Below is the script output.