Functions/Get-IntuneAutopilotAssignmentReport.ps1


function Get-IntuneAutopilotAssignmentReport {
    [CmdletBinding()]
    param (
        # [Parameter()] [ValidateSet("Windows", "iOS", "Android")] [string] $OS,
        # [Parameter()] [switch] $IncludeDefaultIntuneApps
    )

    #$Apps = Get-IntuneMobileApp #-Search adobe
    #$Apps = Get-IntuneMobileApp -Filter "displayName eq '$($AppName)'"
    $AutopilotProfiles = (Invoke-GraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/").value

    $AutopilotAssignments = @()

    foreach ($a in $AutopilotProfiles) {

        $AutopilotProfileAssignments = (Invoke-GraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($a.id)/assignments").value
        $AutopilotProfileAssignments | ForEach-Object {
            $Properties = [ordered]@{
                ProfileId = $a.id
                AadGroup  = $_.target.groupId
                type      = $_.target.'@odata.type'
            }
            $AutopilotAssignments += New-Object -TypeName psobject -Property $Properties
        }

    }
    # $AutopilotProfiles
    # $AutopilotAssignments

    $AadGroups = @()
    $AutopilotAssignments.AadGroup | Select-Object -Unique | ForEach-Object {
        # try {
        # $AadGroups += Get-AzureADGroup -ObjectId $_ -ea 0
        $AadGroups += Get-MgGroup -GroupId $_ -ea 0
        # }
        # catch { }
    }
    $AadGroups = $AadGroups | Sort-Object DisplayName
    # $AadGroups






    $Output = @()

    foreach ($a in $AutopilotProfiles) {

        # $a.id + " " + $a.displayName

        $Properties = [ordered]@{
            displayName = $a.displayName
            id          = $a.id
            # Type = $a.'@odata.type'
            description = $a.description
            AllDevices  = $null
        }
        foreach ($ag in $AadGroups) {
            $Properties.Add($ag.DisplayName, $null)
        }


        $ThisAssignment = $AutopilotAssignments | Where-Object ProfileId -eq $a.id
        # $ThisAssignment

        foreach ($aa in $ThisAssignment) {
            # $aa
            if ($aa.AadGroup) {
                $TargetGroup = $AadGroups | Where-Object Id -eq $aa.AadGroup
                if ($TargetGroup) {
                    if ($aa.type -eq "#microsoft.graph.exclusionGroupAssignmentTarget") {
                        $CurrentIntent = "Excluded"
                    }
                    elseif ($aa.type -eq "#microsoft.graph.groupAssignmentTarget") {
                        $CurrentIntent = "included"
                    }
                    if ($Properties[$TargetGroup.DisplayName]) {
                        $Properties[$TargetGroup.DisplayName] += " / "
                    }
                    $Properties[$TargetGroup.DisplayName] += $CurrentIntent
                }
            }
            else {
                if ($aa.type -eq "#microsoft.graph.allDevicesAssignmentTarget") {
                    $Properties["AllDevices"] = $aa.intent
                }
            }

        }

        $Output += New-Object -TypeName psobject -Property $Properties

    }

    return $Output

}