Private/Get-specEntraIDDeviceID.ps1
Function Get-specEntraIDDeviceID { <# .SYNOPSIS Retrieves the ID of devices from Microsoft Graph API based on their display names. .DESCRIPTION This function retrieves the ID of devices from Microsoft Graph API based on their display names. It requires an access token with appropriate permissions to access Microsoft Graph. .PARAMETER ComputerName Specifies the name(s) of the computer(s) whose ID 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-specEntraIDDeviceID -ComputerName "computer1" -AccessToken "your_access_token_here" Retrieve the ID for a single device with the specified name. .EXAMPLE "computer1" | Get-specEntraIDDeviceID -AccessToken "your_access_token_here" Retrieve the ID for a single device using pipeline input for the computer name. .EXAMPLE $customObject = [pscustomobject]@{ ComputerName = "computer1" AccessToken = "your_access_token_here" } $customObject | Get-specEntraIDDeviceID Retrieve the ID 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) { $url = "https://graph.microsoft.com/v1.0/devices?`$filter=displayName eq '$device'&`$select=id" try { $result = Invoke-RestMethod -Method Get -Uri $url -Headers @{Authorization = "Bearer $($AccessToken)" } -ea Stop } catch { Write-Warning "Unable to obtain ID for $Device Error: $($_.Exception.Message)" continue } [pscustomobject]@{DeviceID = $result.value.id } } } } |