|
| 1 | +function Invoke-ListAppleEnrollmentProfiles { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + Endpoint.MEM.Read |
| 7 | + .DESCRIPTION |
| 8 | + Lists Apple Automated Device Enrollment tokens and enrollment profiles. |
| 9 | + #> |
| 10 | + [CmdletBinding()] |
| 11 | + param($Request, $TriggerMetadata) |
| 12 | + |
| 13 | + $APIName = $Request.Params.CIPPEndpoint |
| 14 | + $Headers = $Request.Headers |
| 15 | + $TenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter |
| 16 | + |
| 17 | + try { |
| 18 | + $DepOnboardingSettings = @(New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/depOnboardingSettings' -tenantid $TenantFilter) |
| 19 | + $Tokens = foreach ($DepSetting in $DepOnboardingSettings) { |
| 20 | + $Token = $DepSetting | Select-Object * |
| 21 | + $Token | Add-Member -NotePropertyName 'daysUntilExpiration' -NotePropertyValue $( |
| 22 | + if ($Token.tokenExpirationDateTime) { |
| 23 | + [math]::Floor(([datetime]$Token.tokenExpirationDateTime - [datetime]::UtcNow).TotalDays) |
| 24 | + } else { |
| 25 | + $null |
| 26 | + } |
| 27 | + ) -Force |
| 28 | + $Token | Add-Member -NotePropertyName 'isExpired' -NotePropertyValue $( |
| 29 | + if ($Token.tokenExpirationDateTime) { ([datetime]$Token.tokenExpirationDateTime) -lt [datetime]::UtcNow } else { $false } |
| 30 | + ) -Force |
| 31 | + $Token |
| 32 | + } |
| 33 | + |
| 34 | + $Profiles = foreach ($DepSetting in $DepOnboardingSettings) { |
| 35 | + if ([string]::IsNullOrWhiteSpace($DepSetting.id)) { continue } |
| 36 | + |
| 37 | + try { |
| 38 | + $EnrollmentProfiles = @(New-GraphGetRequest -uri "https://graph.microsoft.com/beta/deviceManagement/depOnboardingSettings/$($DepSetting.id)/enrollmentProfiles" -tenantid $TenantFilter) |
| 39 | + foreach ($EnrollmentProfile in $EnrollmentProfiles) { |
| 40 | + $ProfileType = $EnrollmentProfile.'@odata.type' |
| 41 | + $Platform = switch -Regex ($ProfileType) { |
| 42 | + 'depMacOSEnrollmentProfile' { 'macOS'; break } |
| 43 | + 'depIOSEnrollmentProfile' { 'iOS/iPadOS'; break } |
| 44 | + 'depVisionOSEnrollmentProfile' { 'visionOS'; break } |
| 45 | + 'depTvOSEnrollmentProfile' { 'tvOS'; break } |
| 46 | + default { 'Unknown' } |
| 47 | + } |
| 48 | + |
| 49 | + $ProfileObject = $EnrollmentProfile | Select-Object * |
| 50 | + $ProfileObject | Add-Member -NotePropertyName 'platform' -NotePropertyValue $Platform -Force |
| 51 | + $ProfileObject | Add-Member -NotePropertyName 'profileType' -NotePropertyValue 'apple' -Force |
| 52 | + $ProfileObject | Add-Member -NotePropertyName 'tokenId' -NotePropertyValue $DepSetting.id -Force |
| 53 | + $ProfileObject | Add-Member -NotePropertyName 'tokenName' -NotePropertyValue $DepSetting.tokenName -Force |
| 54 | + $ProfileObject | Add-Member -NotePropertyName 'appleIdentifier' -NotePropertyValue $DepSetting.appleIdentifier -Force |
| 55 | + $ProfileObject | Add-Member -NotePropertyName 'tokenExpirationDateTime' -NotePropertyValue $DepSetting.tokenExpirationDateTime -Force |
| 56 | + $ProfileObject | Add-Member -NotePropertyName 'tokenType' -NotePropertyValue $DepSetting.tokenType -Force |
| 57 | + $ProfileObject |
| 58 | + } |
| 59 | + } catch { |
| 60 | + $ErrorMessage = Get-CippException -Exception $_ |
| 61 | + Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message "Failed to list Apple ADE profiles for token $($DepSetting.tokenName)" -Sev Warning -LogData $ErrorMessage |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $StatusCode = [HttpStatusCode]::OK |
| 66 | + $Body = @{ |
| 67 | + Results = @{ |
| 68 | + Tokens = @($Tokens) |
| 69 | + Profiles = @($Profiles) |
| 70 | + } |
| 71 | + } |
| 72 | + } catch { |
| 73 | + $StatusCode = [HttpStatusCode]::InternalServerError |
| 74 | + $ErrorMessage = Get-CippException -Exception $_ |
| 75 | + $Body = @{ Results = "Failed to list Apple ADE enrollment profiles: $($ErrorMessage.NormalizedMessage)" } |
| 76 | + Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Body.Results -Sev Error -LogData $ErrorMessage |
| 77 | + } |
| 78 | + |
| 79 | + return ([HttpResponseContext]@{ |
| 80 | + StatusCode = $StatusCode |
| 81 | + Body = $Body |
| 82 | + }) |
| 83 | +} |
0 commit comments