Skip to content

Commit ad9a7c0

Browse files
add groupTag
1 parent 55cc144 commit ad9a7c0

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Endpoint/Autopilot/Invoke-AddAPDevice.ps1

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,36 @@ function Invoke-AddAPDevice {
5151
if ($NewStatus.status -ne 'finished') { throw 'Could not retrieve status of import - This job might still be running. Check the autopilot device list in 10 minutes for the latest status.' }
5252
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $($Request.body.TenantFilter.value) -message "Created Autopilot devices group. Group ID is $GroupName" -Sev 'Info'
5353

54+
# Apply group tags from CSV if any devices have them
55+
$DevicesWithGroupTags = @($rawDevices | Where-Object { $_.groupTag -and $_.groupTag.Trim() -ne '' })
56+
$GroupTagResults = [System.Collections.Generic.List[string]]::new()
57+
if ($DevicesWithGroupTags.Count -gt 0) {
58+
$TenantFilter = $Request.Body.TenantFilter.value
59+
try {
60+
$AutopilotDevices = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeviceIdentities?$top=999' -tenantid $TenantFilter
61+
foreach ($Device in $DevicesWithGroupTags) {
62+
try {
63+
$APDevice = $AutopilotDevices | Where-Object { $_.serialNumber -eq $Device.SerialNumber } | Select-Object -First 1
64+
if ($APDevice) {
65+
$body = @{ groupTag = $Device.groupTag } | ConvertTo-Json
66+
New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeviceIdentities/$($APDevice.id)/UpdateDeviceProperties" -tenantid $TenantFilter -body $body -method POST | Out-Null
67+
$GroupTagResults.Add("Set group tag '$($Device.groupTag)' for device '$($Device.SerialNumber)'")
68+
} else {
69+
$GroupTagResults.Add("Could not find Autopilot device for serial '$($Device.SerialNumber)' to set group tag - device may still be syncing")
70+
}
71+
} catch {
72+
$GroupTagResults.Add("Failed to set group tag for device '$($Device.SerialNumber)': $($_.Exception.Message)")
73+
}
74+
}
75+
} catch {
76+
$GroupTagResults.Add("Failed to retrieve Autopilot devices for group tag assignment: $($_.Exception.Message)")
77+
}
78+
}
79+
5480
[PSCustomObject]@{
55-
Status = 'Import Job Completed'
56-
Devices = @($NewStatus.devicesStatus)
81+
Status = 'Import Job Completed'
82+
Devices = @($NewStatus.devicesStatus)
83+
GroupTagStatus = $GroupTagResults
5784
}
5885
$StatusCode = [HttpStatusCode]::OK
5986
} catch {

0 commit comments

Comments
 (0)