Skip to content

Commit fd32cdc

Browse files
committed
Merge remote-tracking branch 'upstream/dev' into dev
2 parents be42781 + 7e3662c commit fd32cdc

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

Modules/CIPPCore/Public/Set-CIPPDefaultAPDeploymentProfile.ps1

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,31 @@ function Set-CIPPDefaultAPDeploymentProfile {
5858
}
5959
if (!$Profiles) {
6060
if ($PSCmdlet.ShouldProcess($displayName, 'Add Autopilot profile')) {
61+
$Type = 'Add'
6162
$GraphRequest = New-GraphPostRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles' -body $body -tenantid $tenantfilter
6263
Write-LogMessage -user $User -API $APIName -tenant $($tenantfilter) -message "Added Autopilot profile $($displayname)" -Sev 'Info'
6364
}
6465
} else {
65-
#patch the profile
66+
$Type = 'Edit'
6667
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($Profiles.id)" -tenantid $tenantfilter -body $body -type PATCH
67-
$GraphRequest = $Profiles
68+
$GraphRequest = $Profiles | Select-Object -Last 1
6869
}
6970

7071
if ($AssignTo -eq $true) {
7172
$AssignBody = '{"target":{"@odata.type":"#microsoft.graph.allDevicesAssignmentTarget"}}'
7273
if ($PSCmdlet.ShouldProcess($AssignTo, "Assign Autopilot profile $displayname")) {
73-
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($GraphRequest.id)/assignments" -tenantid $tenantfilter -type POST -body $AssignBody
74+
#Get assignments
75+
$Assignments = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($GraphRequest.id)/assignments" -tenantid $tenantfilter
76+
if (!$Assignments) {
77+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($GraphRequest.id)/assignments" -tenantid $tenantfilter -type POST -body $AssignBody
78+
}
7479
Write-LogMessage -user $User -API $APIName -tenant $($tenantfilter) -message "Assigned autopilot profile $($Displayname) to $AssignTo" -Sev 'Info'
7580
}
7681
}
77-
"Successfully added profile for $($tenantfilter)"
82+
"Successfully $($Type)ed profile for $($tenantfilter)"
7883
} catch {
7984
$ErrorMessage = Get-CippException -Exception $_
80-
Write-LogMessage -user $User -API $APIName -tenant $($tenantfilter) -message "Failed adding Autopilot Profile $($Displayname). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
85+
Write-LogMessage -user $User -API $APIName -tenant $($tenantfilter) -message "Failed $($Type)ing Autopilot Profile $($Displayname). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
8186
throw "Failed to add profile for $($tenantfilter): $($ErrorMessage.NormalizedError)"
8287
}
8388
}

Modules/CIPPCore/Public/Set-CIPPIntunePolicy.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function Set-CIPPIntunePolicy {
2121
$CheckExististing = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceAppManagement/$TemplateTypeURL" -tenantid $tenantFilter
2222
if ($displayname -in $CheckExististing.displayName) {
2323
$PostType = 'edited'
24-
$ExistingID = $CheckExististing | Where-Object -Property displayName -EQ $DisplayName
2524
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceAppManagement/$TemplateTypeURL/$($ExistingID.Id)" -tenantid $tenantFilter -type PATCH -body $RawJSON
25+
$CreateRequest = $CheckExististing | Where-Object -Property displayName -EQ $DisplayName
2626
} else {
2727
$PostType = 'added'
2828
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceAppManagement/$TemplateTypeURL" -tenantid $tenantFilter -type POST -body $RawJSON
@@ -36,9 +36,9 @@ function Set-CIPPIntunePolicy {
3636
if ($displayname -in $CheckExististing.displayName) {
3737
$RawJSON = ConvertTo-Json -InputObject ($JSON | Select-Object * -ExcludeProperty 'scheduledActionsForRule') -Depth 20 -Compress
3838
$PostType = 'edited'
39-
$ExistingID = $CheckExististing | Where-Object -Property displayName -EQ $DisplayName
4039
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL/$($ExistingID.Id)" -tenantid $tenantFilter -type PATCH -body $RawJSON
4140
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $($tenantFilter) -message "Updated policy $($DisplayName) to template defaults" -Sev 'info'
41+
$CreateRequest = $CheckExististing | Where-Object -Property displayName -EQ $DisplayName
4242
} else {
4343
$RawJSON = ConvertTo-Json -InputObject $JSON -Depth 20 -Compress
4444
$PostType = 'added'
@@ -52,13 +52,14 @@ function Set-CIPPIntunePolicy {
5252
$CheckExististing = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenantFilter
5353
if ($displayname -in $CheckExististing.displayName) {
5454
$ExistingID = $CheckExististing | Where-Object -Property displayName -EQ $displayname
55-
$ExistingData = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL('$($existingId.id)')/definitionValues" -tenantid $tenantFilter
55+
$ExistingData = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL('$($ExistingID.id)')/definitionValues" -tenantid $tenantFilter
5656
$DeleteJson = $RawJSON | ConvertFrom-Json -Depth 10
5757
$DeleteJson.deletedIds = @($ExistingData.id)
5858
$DeleteJson.added = @()
5959
$DeleteJson = ConvertTo-Json -Depth 10 -InputObject $DeleteJson
60-
$DeleteRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL('$($existingId.id)')/updateDefinitionValues" -tenantid $tenantFilter -type POST -body $DeleteJson
61-
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL('$($existingId.id)')/updateDefinitionValues" -tenantid $tenantFilter -type POST -body $RawJSON
60+
$DeleteRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL('$($ExistingID.id)')/updateDefinitionValues" -tenantid $tenantFilter -type POST -body $DeleteJson
61+
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL('$($ExistingID.id)')/updateDefinitionValues" -tenantid $tenantFilter -type POST -body $RawJSON
62+
$CreateRequest = $CheckExististing | Where-Object -Property displayName -EQ $DisplayName
6263
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $($tenantFilter) -message "Updated policy $($Displayname) to template defaults" -Sev 'info'
6364
$PostType = 'edited'
6465
} else {
@@ -110,8 +111,9 @@ function Set-CIPPIntunePolicy {
110111
$CheckExististing = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenantFilter
111112
if ($DisplayName -in $CheckExististing.name) {
112113
$PostType = 'edited'
113-
$ExistingID = $CheckExististing | Where-Object -Property Name -EQ $DisplayName
114114
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL/$($ExistingID.Id)" -tenantid $tenantFilter -type PUT -body $RawJSON
115+
$CreateRequest = $CheckExististing | Where-Object -Property displayName -EQ $DisplayName
116+
115117
} else {
116118
$PostType = 'added'
117119
$CreateRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/$TemplateTypeURL" -tenantid $tenantFilter -type POST -body $RawJSON
@@ -123,6 +125,7 @@ function Set-CIPPIntunePolicy {
123125
Write-LogMessage -user $ExecutingUser -API $APINAME -tenant $($tenantFilter) -message "$($PostType) policy $($Displayname)" -Sev 'Info'
124126
if ($AssignTo) {
125127
Write-Host "Assigning policy to $($AssignTo) with ID $($CreateRequest.id) and type $TemplateTypeURL for tenant $tenantFilter"
128+
Write-Host "ID is $($CreateRequest.id)"
126129
Set-CIPPAssignedPolicy -GroupName $AssignTo -PolicyId $CreateRequest.id -Type $TemplateTypeURL -TenantFilter $tenantFilter
127130
}
128131
return "Successfully $($PostType) policy for $($tenantFilter) with display name $($Displayname)"

0 commit comments

Comments
 (0)