-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathaddGroupAssignment.ps1
More file actions
45 lines (37 loc) · 1.28 KB
/
addGroupAssignment.ps1
File metadata and controls
45 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$policyId = "<YOUR POLICY OBJECT ID>"
$targetGroup = "<YOUR GROUP OBJECT ID>"
$getUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies/$($policyId)/assignments"
$postUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies/$($policyId)/microsoft.graph.assign"
Connect-MgGraph
try {
$assignments = Invoke-MgGraphRequest -Method GET -Uri $getUri
}
catch {
Write-Warning $_.Exception.Message
}
$groupAssignments = @($assignments.value | Where-Object { $_.target.'@odata.type' -eq "#microsoft.graph.groupAssignmentTarget" })
$alreadyAssigned = $groupAssignments | Where-Object { $_.target.groupId -eq $targetGroup }
if(-not $alreadyAssigned){
$groupAssignments += [PSCustomObject]@{
target = @{
"@odata.type" = "#microsoft.graph.groupAssignmentTarget"
groupId = $targetGroup
}
}
}
$body = @{
assignments = $groupAssignments | ForEach-Object {
@{
target = @{
"@odata.type" = $_.target.'@odata.type'
groupId = $_.target.groupId
}
}
}
} | ConvertTo-Json -Depth 10
try {
Invoke-MgGraphRequest -Method POST -Uri $postUri -Body $body -ContentType "application/json"
}
catch {
Write-Warning $_.Exception.Message
}