|
| 1 | +function Get-CIPPAlertDeviceComplianceGracePeriod { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + #> |
| 6 | + [CmdletBinding()] |
| 7 | + param( |
| 8 | + [Parameter(Mandatory = $false)] |
| 9 | + [Alias('input')] |
| 10 | + $InputValue, |
| 11 | + $TenantFilter |
| 12 | + ) |
| 13 | + try { |
| 14 | + $ExpiresWithinDays = 0 |
| 15 | + if ($null -ne $InputValue.ExpiresWithinDays -and $InputValue.ExpiresWithinDays -ne '') { |
| 16 | + $parsedDays = 0 |
| 17 | + if ([int]::TryParse($InputValue.ExpiresWithinDays.ToString(), [ref]$parsedDays) -and $parsedDays -gt 0) { |
| 18 | + $ExpiresWithinDays = $parsedDays |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + $GraphRequest = New-GraphGETRequest -uri "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices?`$filter=complianceState eq 'inGracePeriod'&`$select=id,deviceName,userPrincipalName,operatingSystem,managedDeviceOwnerType,complianceState,complianceGracePeriodExpirationDateTime,lastSyncDateTime&`$top=999" -tenantid $TenantFilter |
| 23 | + $AlertData = foreach ($Device in $GraphRequest) { |
| 24 | + $Expiration = $Device.complianceGracePeriodExpirationDateTime |
| 25 | + $DaysRemaining = if ($Expiration) { [Math]::Ceiling(([DateTime]$Expiration - (Get-Date).ToUniversalTime()).TotalDays) } else { $null } |
| 26 | + if ($ExpiresWithinDays -gt 0 -and $null -ne $DaysRemaining -and $DaysRemaining -gt $ExpiresWithinDays) { continue } |
| 27 | + |
| 28 | + $Message = if ($null -ne $DaysRemaining) { |
| 29 | + 'Device {0} is in the compliance grace period and will be marked noncompliant on {1} ({2} days remaining)' -f $Device.deviceName, $Expiration, $DaysRemaining |
| 30 | + } else { |
| 31 | + 'Device {0} is in the compliance grace period' -f $Device.deviceName |
| 32 | + } |
| 33 | + |
| 34 | + [PSCustomObject]@{ |
| 35 | + DeviceName = $Device.deviceName |
| 36 | + Id = $Device.id |
| 37 | + UserPrincipalName = $Device.userPrincipalName |
| 38 | + OperatingSystem = $Device.operatingSystem |
| 39 | + OwnerType = $Device.managedDeviceOwnerType |
| 40 | + GracePeriodExpiration = $Expiration |
| 41 | + DaysRemaining = $DaysRemaining |
| 42 | + LastSync = $Device.lastSyncDateTime |
| 43 | + Message = $Message |
| 44 | + Tenant = $TenantFilter |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if ($AlertData) { |
| 49 | + Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -data $AlertData |
| 50 | + } |
| 51 | + } catch { |
| 52 | + $ErrorMessage = Get-CippException -Exception $_ |
| 53 | + Write-LogMessage -API 'Alerts' -tenant $TenantFilter -message "Could not get compliance grace period state for $($TenantFilter): $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage |
| 54 | + } |
| 55 | +} |
0 commit comments