Collectors/ManagedDevices.ps1
|
@{ Name = 'ManagedDevices' FileName = 'managedDevices' ApiVersion = 'v1.0' RequiredScopes = @('DeviceManagementManagedDevices.Read.All') Description = 'Intune managed device inventory: sync recency, encryption, compliance and OS support state.' Collect = { $staleThresholdDays = 30 $devices = @(Invoke-TLGraphRequest -Uri '/v1.0/deviceManagement/managedDevices?$select=id,deviceName,operatingSystem,osVersion,complianceState,lastSyncDateTime,isEncrypted,managementAgent,userPrincipalName&$top=999' -All) $staleThreshold = [DateTime]::UtcNow.AddDays(-1 * $staleThresholdDays) $staleCount = 0; $notEncryptedCount = 0; $nonCompliantCount = 0; $unsupportedOsCount = 0 foreach ($device in $devices) { $isStale = $false if ($device.PSObject.Properties['lastSyncDateTime'] -and $device.lastSyncDateTime) { $isStale = ([DateTime]$device.lastSyncDateTime).ToUniversalTime() -lt $staleThreshold } if ($isStale) { $staleCount++ } if (-not [bool]$device.isEncrypted) { $notEncryptedCount++ } if ([string]$device.complianceState -eq 'noncompliant') { $nonCompliantCount++ } # Windows 10 (builds below 22000) left support in October 2025. $isUnsupportedOs = $false if ([string]$device.operatingSystem -match '(?i)windows' -and [string]$device.osVersion -match '^10\.0\.(\d+)') { $isUnsupportedOs = [int]$Matches[1] -lt 22000 } if ($isUnsupportedOs) { $unsupportedOsCount++ } Add-Member -InputObject $device -NotePropertyName '_tlStale' -NotePropertyValue $isStale -Force Add-Member -InputObject $device -NotePropertyName '_tlUnsupportedOs' -NotePropertyValue $isUnsupportedOs -Force } $byOperatingSystem = [ordered]@{} foreach ($osGroup in ($devices | Group-Object -Property operatingSystem | Sort-Object -Property Name)) { $byOperatingSystem[[string]$osGroup.Name] = $osGroup.Count } @{ staleThresholdDays = $staleThresholdDays summary = [ordered]@{ total = @($devices).Count byOperatingSystem = $byOperatingSystem staleCount = $staleCount notEncryptedCount = $notEncryptedCount nonCompliantCount = $nonCompliantCount unsupportedOsCount = $unsupportedOsCount } devices = $devices } } } |