Functions/Find-AutopilotDevice.ps1


function Find-AutopilotDevice {
    [CmdletBinding()]
    param (
        [Parameter()]
        [ArgumentCompleter( {
                param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
                "Virtual Machine" | ForEach-Object { "`"$_`"" } }
        )]
        [string] $Model,
        [Parameter()] [string] $DeviceName
    )

    Write-Verbose "Getting Autopilot devices"
    $AutopilotDevices = Get-AutopilotDevice
    if ($Model) {
        $AutopilotDevices = $AutopilotDevices | Where-Object model -eq $Model
    }


    Write-Verbose "Getting Azure AD Devices"
    $AzureADdevices = Get-AzureADDevice -All $true -Filter "DeviceOSType eq 'Windows'"
    if ($DeviceName) {
        $AzureADdevices = $AzureADdevices | Where-Object DisplayName -eq $DeviceName
    }

    Write-Verbose "Getting Intune Devices"
    $IntuneDevices = Get-IntuneManagedDevice -Filter "operatingSystem eq 'Windows'" | Get-MSGraphAllPages


    #$AutopilotDevices | Format-Table

    $Result = @()

    foreach ($apd in $AutopilotDevices) {

        $CurrentAzureAdDevice = $AzureADdevices | Where-Object DeviceId -eq $apd.azureAdDeviceId
        $CurrentIntuneDevice = $IntuneDevices | Where-Object id -eq $apd.managedDeviceId

        # $CurrentAzureAdDevice
        # $CurrentIntuneDevice | Format-Table

        if ($CurrentAzureAdDevice) {
            $obj = [ordered]@{
                DeviceNameAAD                     = $CurrentAzureAdDevice.DisplayName
                AutopilotGroupTag                 = $apd.groupTag
                serialNumber                      = $apd.serialNumber
                deploymentProfileAssignmentStatus = $apd.deploymentProfileAssignmentStatus
                AutopilotID                       = $apd.id
                manufacturer                      = $apd.manufacturer
                model                             = $apd.model
                UserPrincipalName                 = $CurrentIntuneDevice.userPrincipalName
                AutopilotDisplayName              = $apd.displayName
                AADApproximateLastLogonTimeStamp  = $CurrentAzureAdDevice.ApproximateLastLogonTimeStamp
                AADAccountEnabled                 = $CurrentAzureAdDevice.AccountEnabled
                IntuneenrolledDateTime            = $CurrentIntuneDevice.enrolledDateTime
                IntunelastSyncDateTime            = $CurrentIntuneDevice.lastSyncDateTime
                IntuneID                          = $CurrentIntuneDevice.id
                AzureAdID                         = $CurrentAzureAdDevice.DeviceId
                DeviceNameIntune                  = $CurrentIntuneDevice.deviceName
                skuNumber                         = $apd.skuNumber
            }

            $Result += new-object -Type Psobject -property $obj

            # Write-Verbose $obj.DeviceNameAAD
        }

    }

    $Result = $Result | Sort-Object DeviceNameAAD

    return $Result

}



<#
 
# Autopilot
 
id
deploymentProfileAssignmentStatus
deploymentProfileAssignmentDetailedStatus
deploymentProfileAssignedDateTime
groupTag
purchaseOrderIdentifier
serialNumber
productKey
manufacturer
model
enrollmentState
lastContactedDateTime
addressableUserName
userPrincipalName
resourceName
skuNumber
systemFamily
azureActiveDirectoryDeviceId
azureAdDeviceId
managedDeviceId
displayName
 
 
# Azure AD
 
ApproximateLastLogonTimeStamp
ComplianceExpiryTime
DeviceId
DeviceMetadata
DeviceObjectVersion
DeviceOSType
DeviceOSVersion
DevicePhysicalIds
DeviceTrustType
DirSyncEnabled
DisplayName
IsCompliant
IsManaged
LastDirSyncTime
ProfileType
SystemLabels
DeletionTimestamp
ObjectId
ObjectType
AccountEnabled
AlternativeSecurityIds
ApproximateLastLogonTimeStamp
ComplianceExpiryTime
DeviceId
DeviceMetadata
DeviceObjectVersion
DeviceOSType
DeviceOSVersion
DevicePhysicalIds
DeviceTrustType
DirSyncEnabled
DisplayName
IsCompliant
IsManaged
LastDirSyncTime
ProfileType
SystemLabels
 
 
 
# Intune
 
id
userId
deviceName
managedDeviceOwnerType
enrolledDateTime
lastSyncDateTime
operatingSystem
complianceState
jailBroken
managementAgent
osVersion
easActivated
easDeviceId
easActivationDateTime
azureADRegistered
deviceEnrollmentType
activationLockBypassCode
emailAddress
deviceRegistrationState
deviceCategoryDisplayName
isSupervised
exchangeLastSuccessfulSyncDateTime
exchangeAccessState
exchangeAccessStateReason
remoteAssistanceSessionUrl
remoteAssistanceSessionErrorDetails
isEncrypted
userPrincipalName
model
manufacturer
imei
complianceGracePeriodExpirationDateTime
serialNumber
phoneNumber
androidSecurityPatchLevel
userDisplayName
configurationManagerClientEnabledFeatures
wiFiMacAddress
deviceHealthAttestationState
subscriberCarrier
meid
totalStorageSpaceInBytes
freeStorageSpaceInBytes
managedDeviceName
partnerReportedThreatState
iccid
udid
notes
ethernetMacAddress
physicalMemoryInBytes
deviceActionResults
 
#>