Public/Set-IBMIntuneDeviceName.ps1
function Set-IBMIntuneDeviceName { <# .SYNOPSIS Changes the device name for Intune managed devices based on specified criteria. .DESCRIPTION The Set-IBMIntuneDeviceName function allows you to change the device name for Intune managed devices. You can specify individual devices by DeviceId, GroupName, DeviceName, or OS. Additionally, you can choose to change names for all devices or select devices/groups interactively. .NOTES Author: Florian Salzmann | @FlorianSLZ | https://scloud.work Version: 1.0 Date: 2024-08-02 #> param ( [parameter(Mandatory = $false, HelpMessage = "Specify the ID of the individual device to rotate the name for.")] [string]$DeviceId, [parameter(Mandatory = $false, HelpMessage = "Specify the name of the group to which the devices belong.")] [string]$GroupName, [parameter(Mandatory = $false, HelpMessage = "Specify the name of the individual device to rotate the name for.")] [string]$DeviceName, [parameter(Mandatory = $false, HelpMessage = "Specify the operating system of the devices to rotate the name for. For example, 'Windows' or 'iOS'.")] [string]$OS, [parameter(Mandatory = $false, HelpMessage = "Rotate the name for all devices managed by Intune.")] [switch]$AllDevices, [parameter(Mandatory = $false, HelpMessage = "Select specific devices interactively to rotate the name for.")] [switch]$SelectDevices, [parameter(Mandatory = $false, HelpMessage = "Select a specific group of devices interactively to rotate the name for.")] [switch]$SelectGroup, [parameter(Mandatory=$true, HelpMessage = "Specify the prefix for the new device name.")] [string]$Prefix, [parameter(Mandatory=$false, HelpMessage = "Specify the type of suffix to append to the prefix. Choose between 'SerialNumber' or 'UniqueNumber'.")] [string]$SuffixType, [parameter(Mandatory=$false, HelpMessage = "Specify the length of the unique number to generate if 'UniqueNumber' is selected for SuffixType.")] [ValidateSet("SerialNumber","UniqueNumber")] [int]$UniqueNumberLength = 4 ) # Get device IDs based on provided criteria if($AllDevices){ $deviceIds = Get-IntuneDeviceIDs -AllDevices }elseif($SelectDevices){ $deviceIds = Get-IntuneDeviceIDs -SelectDevices }elseif($SelectGroup){ $deviceIds = Get-IntuneDeviceIDs -SelectGroup }else{ $deviceIds = Get-IntuneDeviceIDs -DeviceId $DeviceId -GroupName $GroupName -DeviceName $DeviceName -OS $OS } if (-not $deviceIds) { Write-Warning "No devices found based on the provided criteria." return } if ($SuffixType -eq "SerialNumber") { $existingDevices = Invoke-IBMPagingRequest -Uri "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices" } # Set Intune Device Name for each device $counter = 0 foreach ($deviceId in $deviceIds) { $counter++ Write-Progress -Id 0 -Activity "Set Intune Device Name" -Status "Processing $($counter) of $($deviceIds.count)" -CurrentOperation $computer -PercentComplete (($counter/$deviceIds.Count) * 100) # Get the device details to retrieve the serial number if needed if ($SuffixType -eq "SerialNumber") { $device = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/$DeviceId" if ($null -eq $device) { Write-Error "Device not found with ID $DeviceId" return } $serialNumber = $device.serialNumber if ($null -eq $serialNumber) { Write-Error "Serial number not found for device ID $DeviceId" return } $newDeviceName = "$Prefix$serialNumber" } elseif ($SuffixType -eq "UniqueNumber") { do { $suffix = -join ((48..57) | Get-Random -Count $UniqueNumberLength | ForEach-Object {[char]$_}) $newDeviceName = "$Prefix$suffix" } while (-not ($existingDevices.deviceName -contains $newDeviceName)) } else { $newDeviceName = "$Prefix" } # Prepare the request body $body = @{ deviceName = $newDeviceName } # Convert the body to JSON $jsonBody = $body | ConvertTo-Json # Update the device name Invoke-MgGraphRequest -Method PATCH -Uri "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/$DeviceId" -Body $jsonBody -ContentType "application/json" Write-Output "Device name set to $newDeviceName for device ID $DeviceId" } } |