Skip to content

Commit c5d51a1

Browse files
Merge pull request KelvinTegelaar#1403 from Ren-Roros-Digital/fix-selfdeployment
chore: fix up AutoPilotProfile
2 parents 978b030 + bed5ba0 commit c5d51a1

2 files changed

Lines changed: 75 additions & 54 deletions

File tree

Modules/CIPPCore/Public/Set-CIPPDefaultAPDeploymentProfile.ps1

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ function Set-CIPPDefaultAPDeploymentProfile {
22
[CmdletBinding(SupportsShouldProcess = $true)]
33
param(
44
$tenantFilter,
5-
$displayname,
5+
$displayName,
66
$description,
77
$devicenameTemplate,
88
$allowWhiteGlove,
99
$CollectHash,
10-
$usertype,
10+
$userType,
1111
$DeploymentMode,
1212
$hideChangeAccount,
1313
$AssignTo,
1414
$hidePrivacy,
1515
$hideTerms,
16-
$Autokeyboard,
16+
$AutoKeyboard,
1717
$Headers,
1818
$Language = 'os-default',
1919
$APIName = 'Add Default Enrollment Status Page'
@@ -24,9 +24,9 @@ function Set-CIPPDefaultAPDeploymentProfile {
2424
try {
2525
$ObjBody = [pscustomobject]@{
2626
'@odata.type' = '#microsoft.graph.azureADWindowsAutopilotDeploymentProfile'
27-
'displayName' = "$($displayname)"
27+
'displayName' = "$($displayName)"
2828
'description' = "$($description)"
29-
'deviceNameTemplate' = "$($DeviceNameTemplate)"
29+
'deviceNameTemplate' = "$($devicenameTemplate)"
3030
'language' = "$($Language)"
3131
'enableWhiteGlove' = $([bool]($allowWhiteGlove))
3232
'deviceType' = 'windowsPc'
@@ -38,19 +38,19 @@ function Set-CIPPDefaultAPDeploymentProfile {
3838
'escapeLinkHidden' = $([bool]($hideChangeAccount))
3939
'privacySettingsHidden' = $([bool]($hidePrivacy))
4040
'eulaHidden' = $([bool]($hideTerms))
41-
'userType' = "$usertype"
42-
'keyboardSelectionPageSkipped' = $([bool]($Autokeyboard))
41+
'userType' = "$userType"
42+
'keyboardSelectionPageSkipped' = $([bool]($AutoKeyboard))
4343
}
4444
}
4545
$Body = ConvertTo-Json -InputObject $ObjBody
4646

47-
$Profiles = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles' -tenantid $tenantfilter | Where-Object -Property displayName -EQ $displayname
47+
$Profiles = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles' -tenantid $tenantFilter | Where-Object -Property displayName -EQ $displayName
4848
if ($Profiles.count -gt 1) {
4949
$Profiles | ForEach-Object {
5050
if ($_.id -ne $Profiles[0].id) {
5151
if ($PSCmdlet.ShouldProcess($_.displayName, 'Delete duplicate Autopilot profile')) {
52-
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($_.id)" -tenantid $tenantfilter -type DELETE
53-
Write-LogMessage -Headers $User -API $APIName -tenant $($tenantfilter) -message "Deleted duplicate Autopilot profile $($displayname)" -Sev 'Info'
52+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($_.id)" -tenantid $tenantFilter -type DELETE
53+
Write-LogMessage -Headers $User -API $APIName -tenant $($tenantFilter) -message "Deleted duplicate Autopilot profile $($displayName)" -Sev 'Info'
5454
}
5555
}
5656
}
@@ -59,30 +59,30 @@ function Set-CIPPDefaultAPDeploymentProfile {
5959
if (!$Profiles) {
6060
if ($PSCmdlet.ShouldProcess($displayName, 'Add Autopilot profile')) {
6161
$Type = 'Add'
62-
$GraphRequest = New-GraphPostRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles' -body $body -tenantid $tenantfilter
63-
Write-LogMessage -Headers $User -API $APIName -tenant $($tenantfilter) -message "Added Autopilot profile $($displayname)" -Sev 'Info'
62+
$GraphRequest = New-GraphPostRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles' -body $body -tenantid $tenantFilter
63+
Write-LogMessage -Headers $User -API $APIName -tenant $($tenantFilter) -message "Added Autopilot profile $($displayName)" -Sev 'Info'
6464
}
6565
} else {
6666
$Type = 'Edit'
67-
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($Profiles.id)" -tenantid $tenantfilter -body $body -type PATCH
67+
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($Profiles.id)" -tenantid $tenantFilter -body $body -type PATCH
6868
$GraphRequest = $Profiles | Select-Object -Last 1
6969
}
7070

7171
if ($AssignTo -eq $true) {
7272
$AssignBody = '{"target":{"@odata.type":"#microsoft.graph.allDevicesAssignmentTarget"}}'
73-
if ($PSCmdlet.ShouldProcess($AssignTo, "Assign Autopilot profile $displayname")) {
73+
if ($PSCmdlet.ShouldProcess($AssignTo, "Assign Autopilot profile $displayName")) {
7474
#Get assignments
75-
$Assignments = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($GraphRequest.id)/assignments" -tenantid $tenantfilter
75+
$Assignments = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($GraphRequest.id)/assignments" -tenantid $tenantFilter
7676
if (!$Assignments) {
77-
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($GraphRequest.id)/assignments" -tenantid $tenantfilter -type POST -body $AssignBody
77+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles/$($GraphRequest.id)/assignments" -tenantid $tenantFilter -type POST -body $AssignBody
7878
}
79-
Write-LogMessage -Headers $User -API $APIName -tenant $($tenantfilter) -message "Assigned autopilot profile $($Displayname) to $AssignTo" -Sev 'Info'
79+
Write-LogMessage -Headers $User -API $APIName -tenant $tenantFilter -message "Assigned autopilot profile $($displayName) to $AssignTo" -Sev 'Info'
8080
}
8181
}
82-
"Successfully $($Type)ed profile for $($tenantfilter)"
82+
"Successfully $($Type)ed profile for $tenantFilter"
8383
} catch {
8484
$ErrorMessage = Get-CippException -Exception $_
85-
Write-LogMessage -Headers $User -API $APIName -tenant $($tenantfilter) -message "Failed $($Type)ing Autopilot Profile $($Displayname). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
86-
throw "Failed to add profile for $($tenantfilter): $($ErrorMessage.NormalizedError)"
85+
Write-LogMessage -Headers $User -API $APIName -tenant $tenantFilter -message "Failed $($Type)ing Autopilot Profile $($displayName). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
86+
throw "Failed to add profile for $($tenantFilter): $($ErrorMessage.NormalizedError)"
8787
}
8888
}

Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardAutopilotProfile.ps1

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,49 +40,49 @@ function Invoke-CIPPStandardAutopilotProfile {
4040
https://docs.cipp.app/user-documentation/tenant/standards/list-standards/
4141
#>
4242
param($Tenant, $Settings)
43-
##$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'APConfig'
4443

45-
# Check if profile exists
46-
$ProfileExists = $false
44+
# Get the current configuration
4745
try {
48-
$Profiles = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles' -tenantid $Tenant
49-
$ProfileExists = ($Profiles.displayName -contains $settings.DisplayName)
50-
} catch {
51-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
52-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to check Autopilot profiles: $ErrorMessage" -sev 'Error'
53-
}
46+
$CurrentConfig = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeploymentProfiles' -tenantid $Tenant |
47+
Where-Object { $_.displayName -eq $Settings.DisplayName } |
48+
Select-Object -Property displayName, description, deviceNameTemplate, language, enableWhiteGlove, extractHardwareHash, outOfBoxExperienceSetting, preprovisioningAllowed
5449

55-
if ($Settings.report -eq $true) {
56-
$state = $ProfileExists -eq $true ? $true : $ProfileExists
57-
Set-CIPPStandardsCompareField -FieldName 'standards.AutopilotProfile' -FieldValue $state -TenantFilter $tenant
58-
Add-CIPPBPAField -FieldName 'AutopilotProfile' -FieldValue $ProfileExists -StoreAs bool -Tenant $tenant
59-
}
50+
if ($Settings.NotLocalAdmin -eq $true) { $userType = 'Standard' } else { $userType = 'Administrator' }
51+
if ($Settings.SelfDeployingMode -eq $true) { $DeploymentMode = 'shared' } else { $DeploymentMode = 'singleUser' }
52+
if ($Settings.AllowWhiteGlove -eq $true) {$Settings.HideChangeAccount = $true}
6053

61-
if ($Settings.alert -eq $true) {
62-
if ($ProfileExists) {
63-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Autopilot profile '$($settings.DisplayName)' exists" -sev Info
64-
} else {
65-
Write-StandardsAlert -message "Autopilot profile '$($settings.DisplayName)' does not exist" -object @{ProfileName = $settings.DisplayName } -tenant $tenant -standardName 'AutopilotProfile' -standardId $Settings.standardId
66-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Autopilot profile '$($settings.DisplayName)' does not exist" -sev Info
67-
}
54+
$StateIsCorrect = ($CurrentConfig.displayName -eq $Settings.DisplayName) -and
55+
($CurrentConfig.description -eq $Settings.Description) -and
56+
($CurrentConfig.deviceNameTemplate -eq $Settings.DeviceNameTemplate) -and
57+
([string]::IsNullOrWhiteSpace($CurrentConfig.language) -and [string]::IsNullOrWhiteSpace($Settings.Languages.value) -or $CurrentConfig.language -eq $Settings.Languages.value) -and
58+
($CurrentConfig.enableWhiteGlove -eq $Settings.AllowWhiteGlove) -and
59+
($CurrentConfig.extractHardwareHash -eq $Settings.CollectHash) -and
60+
($CurrentConfig.outOfBoxExperienceSetting.deviceUsageType -eq $DeploymentMode) -and
61+
($CurrentConfig.outOfBoxExperienceSetting.escapeLinkHidden -eq $Settings.HideChangeAccount) -and
62+
($CurrentConfig.outOfBoxExperienceSetting.privacySettingsHidden -eq $Settings.HidePrivacy) -and
63+
($CurrentConfig.outOfBoxExperienceSetting.eulaHidden -eq $Settings.HideTerms) -and
64+
($CurrentConfig.outOfBoxExperienceSetting.userType -eq $userType) -and
65+
($CurrentConfig.outOfBoxExperienceSetting.keyboardSelectionPageSkipped -eq $Settings.AutoKeyboard)
66+
}
67+
catch {
68+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
69+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to check Autopilot profile: $ErrorMessage" -sev Error
70+
$StateIsCorrect = $false
6871
}
6972

73+
# Remediate if the state is not correct
7074
If ($Settings.remediate -eq $true) {
71-
if ($ProfileExists) {
72-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Autopilot profile '$($settings.DisplayName)' already exists" -sev Info
75+
if ($StateIsCorrect -eq $true) {
76+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Autopilot profile '$($Settings.DisplayName)' already exists" -sev Info
7377
} else {
7478
try {
75-
Write-Host $($settings | ConvertTo-Json -Depth 100)
76-
if ($settings.NotLocalAdmin -eq $true) { $usertype = 'Standard' } else { $usertype = 'Administrator' }
77-
$DeploymentMode = if ($settings.DeploymentMode -eq 'true') { 'shared' } else { 'singleUser' }
78-
7979
$Parameters = @{
80-
tenantFilter = $tenant
81-
displayname = $settings.DisplayName
82-
description = $settings.Description
83-
usertype = $usertype
80+
tenantFilter = $Tenant
81+
displayName = $Settings.DisplayName
82+
description = $Settings.Description
83+
userType = $userType
8484
DeploymentMode = $DeploymentMode
85-
assignto = $settings.AssignToAllDevices
85+
AssignTo = $Settings.AssignToAllDevices
8686
devicenameTemplate = $Settings.DeviceNameTemplate
8787
allowWhiteGlove = $Settings.AllowWhiteGlove
8888
CollectHash = $Settings.CollectHash
@@ -94,12 +94,33 @@ function Invoke-CIPPStandardAutopilotProfile {
9494
}
9595

9696
Set-CIPPDefaultAPDeploymentProfile @Parameters
97-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Created Autopilot profile '$($settings.DisplayName)'" -sev Info
97+
if ($null -eq $CurrentConfig) {
98+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Created Autopilot profile '$($Settings.DisplayName)'" -sev Info
99+
} else {
100+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Updated Autopilot profile '$($Settings.DisplayName)'" -sev Info
101+
}
98102
} catch {
99103
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
100-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to create Autopilot profile: $ErrorMessage" -sev 'Error'
104+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to create Autopilot profile: $ErrorMessage" -sev 'Error'
101105
throw $ErrorMessage
102106
}
103107
}
104108
}
109+
110+
# Report
111+
If ($Settings.report -eq $true) {
112+
$FieldValue = $StateIsCorrect -eq $true ? $true : $CurrentConfig
113+
Set-CIPPStandardsCompareField -FieldName 'standards.AutopilotProfile' -FieldValue $FieldValue -TenantFilter $Tenant
114+
Add-CIPPBPAField -FieldName 'AutopilotProfile' -FieldValue [bool]$StateIsCorrect -StoreAs bool -Tenant $Tenant
115+
}
116+
117+
# Alert
118+
If ($Settings.alert -eq $true) {
119+
If ($StateIsCorrect -eq $true) {
120+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Autopilot profile '$($Settings.DisplayName)' exists" -sev Info
121+
} else {
122+
Write-StandardsAlert -message "Autopilot profile '$($Settings.DisplayName)' do not match expected configuration" -object $CurrentConfig -tenant $Tenant -standardName 'AutopilotProfile' -standardId $Settings.standardId
123+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Autopilot profile '$($Settings.DisplayName)' do not match expected configuration" -sev Info
124+
}
125+
}
105126
}

0 commit comments

Comments
 (0)