I'm trying to get all iOS devices from Intune with Graph.
Whent testing in Graph Explorer with
"https://graph.microsoft.com/v1.0/deviceManagement/managedDevices?$filter=startswith(operatingSystem, 'iOS')&$select=UserPrincipalName,deviceName,managedDeviceOwnerType,managementState,operatingSystem,complianceState,osVersion,model,manufacturer,imei,serialNumber,phoneNumber,userDisplayName,managementCertificateExpirationDate,emailAddress,id"
I get the right result (one iOS device). But when my script it also shows the Windows devices
# A script to fetch iOS device data from the Microsoft Graph and export it to xlsx file.
#
CLS
$AppId = -
$TenantId = -
$AppSecret = -
# Construct URI and body needed for authentication
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$body = @{
client_id = $AppId
scope = "https://graph.microsoft.com/.default"
client_secret = $AppSecret
grant_type = "client_credentials" }
# Get OAuth 2.0 Token
$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
# Unpack Access Token
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
# Base URL
$headers = @{Authorization = "Bearer $token"}
# Get Device data
Write-Host "Accessing the Graph to get user sign-in data..."
$URI = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices?$filter=startswith(operatingSystem, 'iOS')&$select=UserPrincipalName,deviceName,managedDeviceOwnerType,managementState,operatingSystem,complianceState,osVersion,model,manufacturer,imei,serialNumber,phoneNumber,userDisplayName,managementCertificateExpirationDate,emailAddress,id"
$SignInData = (Invoke-RestMethod -Uri $URI -Headers $Headers -Method Get -ContentType "application/json")
$Report = [System.Collections.Generic.List[Object]]::new()
Foreach ($Device in $SignInData.Value) {
$ReportLine = [PSCustomObject] @{
UPN = $Device.UserPrincipalName
DeviceName = $Device.deviceName
mdType = $Device.managedDeviceOwnerType
ManagementState = $Device.managementState
OperatingSystem = $Device.operatingSystem
ComplianceState = $Device.complianceState
osVersion = $Device.osVersion
Model = $Device.model
Manufacturer = $Device.manufacturer
IMEI = $Device.imei
SerialNumber = $Device.serialNumber
PhoneNumber = $Device.phoneNumber
UserDisplayName = $Device.userDisplayName
MDMCertificateExp = $Device.managementCertificateExpirationDate
Email = $Device.emailAddress
Id = $Device.Id }
$Report.Add($ReportLine)
} # End ForEach
Write-Host "All done. " $Report.Count "accounts processed - output available in c:\Temp\ReportiOSDevices.xlsx."
$Report | Export-Excel c:\Temp\ReportiOSDevices.xlsx