Public/Get-pseEntraIDDeviceGroup.ps1

function Get-pseEntraIDDeviceGroup {
    <#
    .SYNOPSIS
    Retrieves group membership information for devices from Microsoft Graph API using their IDs.
 
    .DESCRIPTION
    This function retrieves group membership information for devices from Microsoft Graph API using their IDs. It requires an access token with appropriate permissions to access Microsoft Graph.
 
    .PARAMETER ComputerName
    Specifies the name(s) of the computer(s) whose group membership information is to be retrieved. This parameter accepts input from the pipeline. If not specified, the default value is set to the name of the local computer.
 
    .PARAMETER AccessToken
    Specifies the access token required to authenticate with Microsoft Graph API. This parameter is mandatory.
 
    .EXAMPLE
    Get-pseEntraIDDeviceGroup -ComputerName "computer1" -AccessToken "your_access_token_here"
    Retrieve group membership information for a single device with the specified name.
 
    .EXAMPLE
    "computer1" | Get-pseEntraIDDeviceGroup -AccessToken "your_access_token_here"
    Retrieve group membership information for a single device using pipeline input for the computer name.
 
    .EXAMPLE
    $customObject = [pscustomobject]@{
        ComputerName = "computer1"
        AccessToken = "your_access_token_here"
    }
    $customObject | Get-pseEntraIDDeviceGroup
    Retrieve group membership information for device(s) whose names are contained in a custom object sent through the pipeline.
 
    .NOTES
    Author: owen.heaume
    Version: 1.0.0
    #>


    [cmdletbinding()]
    param(
        [parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]$ComputerName = $ENV:COMPUTERNAME,

        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string]$AccessToken
    )

    Begin { }

    process {
        foreach ($device in $ComputerName) {
            #Call the private function to get the device ID
            $deviceID = (Get-specEntraIDDeviceID -ComputerName $device -AccessToken $AccessToken).deviceID

            $url = "https://graph.microsoft.com/v1.0/devices/$deviceID/memberOf"
            do {
                try {
                    $result = Invoke-RestMethod -Method Get -Uri $url -Headers @{Authorization = "Bearer $($AccessToken)" } -ea Stop -UseBasicParsing
                    $allGroups += $result.value
                    $url = $result.'@odata.nextLink'
                } catch {
                    Write-Error "$User Error: $($_.Exception.Message)"
                    $url = $null
                }
            } while ($url)

            $allGroups | % { [pscustomobject]@{
                    Device       = $device
                    DeviceID     = $deviceID
                    GroupID      = $_.id
                    DisplayName  = $_.displayName
                    Description  = $_.description
                    MailEnabled  = $_.mailEnabled
                    CreationDate = $_.createdDateTime
                }
            }
        }
    }
}