Microsoft Graph的 PowerShell脚本输出结果不正确
我想用Graph从 Intune获取所有iOS设备。
在Graph Explorer测试时,使用
"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"
我得到了正确的结果(只有一个iOS设备)。但当我运行脚本时,它也会显示Windows设备。
# 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
解决方案
我缺少转义字符 ` and $filter and $select,它们实际上已从Uri中被移除:
https://graph.microsoft.com/v1.0/deviceManagement/managedDevices?`$filter=(operatingSystem eq 'iOS')&`$select=...
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。