Skip to content

Commit 659bdad

Browse files
adds ability for compare.
1 parent 935faa7 commit 659bdad

File tree

2 files changed

+70
-32
lines changed

2 files changed

+70
-32
lines changed

Modules/CIPPCore/Public/Set-CIPPIntunePolicy.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function Set-CIPPIntunePolicy {
1111
$APINAME,
1212
$tenantFilter
1313
)
14+
$APINAME = 'Set-CIPPIntunePolicy'
1415
#connect to table, get replacement map. This is for future usage. The replacement map will allow users to create custom vars that get replaced by the actual values per tenant. Example:
1516
# %WallPaperPath% gets replaced by RowKey WallPaperPath which is set to C:\Wallpapers for tenant 1, and D:\Wallpapers for tenant 2
1617
$ReplaceTable = Get-CIPPTable -tablename 'CippReplacemap'

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

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,49 +35,86 @@ function Invoke-CIPPStandardIntuneTemplate {
3535
$Table = Get-CippTable -tablename 'templates'
3636
$Filter = "PartitionKey eq 'IntuneTemplate'"
3737
$Request = @{body = $null }
38-
$Request.body = (Get-CIPPAzDataTableEntity @Table -Filter $Filter | Where-Object -Property RowKey -Like "$($Settings.TemplateList.value)*").JSON | ConvertFrom-Json
39-
$displayname = $request.body.Displayname
40-
$description = $request.body.Description
41-
$RawJSON = $Request.body.RawJSON
42-
$ExistingPolicy = Get-CIPPIntunePolicy -tenantFilter $Tenant -DisplayName $displayname -TemplateType $Request.body.Type
43-
if ($ExistingPolicy) {
44-
$ReplaceTable = Get-CIPPTable -tablename 'CippReplacemap'
45-
$ReplaceMap = Get-CIPPAzDataTableEntity @ReplaceTable -Filter "PartitionKey eq '$tenantFilter'"
46-
if ($ReplaceMap) {
47-
foreach ($Replace in $ReplaceMap) {
48-
$String = '%{0}%' -f $Replace.RowKey
49-
$RawJSON = $RawJSON -replace $String, $Replace.Value
50-
}
38+
$TenantList = Get-Tenants -TenantFilter $tenantFilter
39+
40+
$CompareList = foreach ($Template in $Settings) {
41+
Write-Host "working on template: $($Template | ConvertTo-Json)"
42+
$Request.body = (Get-CIPPAzDataTableEntity @Table -Filter $Filter | Where-Object -Property RowKey -Like "$($Template.TemplateList.value)*").JSON | ConvertFrom-Json -ErrorAction SilentlyContinue
43+
if ($Request.body -eq $null) {
44+
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to find template $($Template.TemplateList.value). Has this Intune Template been deleted?" -sev 'Error'
45+
continue
5146
}
52-
$Tenant = Get-Tenants -TenantFilter $tenantFilter
53-
$RawJSON = $RawJSON -replace '%tenantid%', $Tenant.customerId
54-
$RawJSON = $RawJSON -replace '%tenantfilter%', $Tenant.defaultDomainName
55-
$RawJSON = $RawJSON -replace '%tenantname%', $Tenant.displayName
47+
$displayname = $request.body.Displayname
48+
$description = $request.body.Description
49+
$RawJSON = $Request.body.RawJSON
50+
$ExistingPolicy = Get-CIPPIntunePolicy -tenantFilter $Tenant -DisplayName $displayname -TemplateType $Request.body.Type
51+
if ($ExistingPolicy) {
52+
$ReplaceTable = Get-CIPPTable -tablename 'CippReplacemap'
53+
$ReplaceMap = Get-CIPPAzDataTableEntity @ReplaceTable -Filter "PartitionKey eq '$tenant'"
54+
if ($ReplaceMap) {
55+
foreach ($Replace in $ReplaceMap) {
56+
$String = '%{0}%' -f $Replace.RowKey
57+
$RawJSON = $RawJSON -replace $String, $Replace.Value
58+
}
59+
}
60+
$RawJSON = $RawJSON -replace '%tenantid%', $TenantList.customerId
61+
$RawJSON = $RawJSON -replace '%tenantfilter%', $TenantLists.defaultDomainName
62+
$RawJSON = $RawJSON -replace '%tenantname%', $TenantList.displayName
5663

57-
$JSONExistingPolicy = $ExistingPolicy.cippconfiguration | ConvertFrom-Json
58-
$JSONTemplate = $RawJSON | ConvertFrom-Json
59-
$Compare = Compare-CIPPIntuneObject -ReferenceObject $JSONTemplate -DifferenceObject $JSONExistingPolicy -compareType $Request.body.Type
64+
$JSONExistingPolicy = $ExistingPolicy.cippconfiguration | ConvertFrom-Json
65+
$JSONTemplate = $RawJSON | ConvertFrom-Json
66+
$Compare = Compare-CIPPIntuneObject -ReferenceObject $JSONTemplate -DifferenceObject $JSONExistingPolicy -compareType $Request.body.Type
67+
if ($Compare) {
68+
[PSCustomObject]@{
69+
MatchFailed = $true
70+
displayname = $displayname
71+
description = $description
72+
compare = $Compare
73+
rawJSON = $RawJSON
74+
body = $Request.body
75+
assignTo = $Template.AssignTo
76+
excludeGroup = $Template.excludeGroup
77+
remediate = $Template.remediate
78+
}
79+
} else {
80+
[PSCustomObject]@{
81+
MatchFailed = $false
82+
displayname = $displayname
83+
description = $description
84+
compare = $Compare
85+
rawJSON = $RawJSON
86+
body = $Request.body
87+
assignTo = $Template.AssignTo
88+
excludeGroup = $Template.excludeGroup
89+
remediate = $Template.remediate
90+
}
91+
}
92+
}
6093
}
6194

6295
If ($Settings.remediate -eq $true) {
6396
Write-Host 'starting template deploy'
64-
Write-Host "The full settings are $($Settings | ConvertTo-Json)"
65-
try {
66-
$Settings.customGroup ? ($Settings.AssignTo = $Settings.customGroup) : $null
67-
Set-CIPPIntunePolicy -TemplateType $Request.body.Type -Description $description -DisplayName $displayname -RawJSON $RawJSON -AssignTo $Settings.AssignTo -ExcludeGroup $Settings.excludeGroup -tenantFilter $Tenant
68-
} catch {
69-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
70-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to create or update Intune Template $displayname, Error: $ErrorMessage" -sev 'Error'
97+
foreach ($Template in $CompareList | Where-Object -Property remediate -EQ $true) {
98+
Write-Host "working on template deploy: $($Template | ConvertTo-Json)"
99+
try {
100+
$Template.customGroup ? ($Template.AssignTo = $Template.customGroup) : $null
101+
Set-CIPPIntunePolicy -TemplateType $Template.body.Type -Description $description -DisplayName $displayname -RawJSON $RawJSON -AssignTo $Template.AssignTo -ExcludeGroup $Template.excludeGroup -tenantFilter $Tenant
102+
103+
} catch {
104+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
105+
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to create or update Intune Template $PolicyName, Error: $ErrorMessage" -sev 'Error'
106+
}
71107
}
72108

73109
}
74110

75111
if ($Settings.alert) {
76-
#Replace the alert method used in standards with a prettier one, link to the report/template, link to a compare. extended table. etc
77-
if ($compare) {
78-
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Policy $($displayname) does not match the expected configuration." -sev Alert
79-
} else {
80-
$ExistingPolicy ? (Write-LogMessage -API 'Standards' -tenant $Tenant -message "Policy $($displayname) has the correct configuration." -sev Info) : (Write-LogMessage -API 'Standards' -tenant $Tenant -message "Policy $($displayname) is missing." -sev Alert)
112+
foreach ($Template in $CompareList) {
113+
if ($Template.compare) {
114+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Template $($Template.displayname) does not match the expected configuration: $($template.compare | ConvertTo-Json)" -sev Alert
115+
} else {
116+
$ExistingPolicy ? (Write-LogMessage -API 'Standards' -tenant $Tenant -message "Template $($Template.displayname) has the correct configuration." -sev Info) : (Write-LogMessage -API 'Standards' -tenant $Tenant -message "Template $($Template.displayname) is missing." -sev Alert)
117+
}
81118
}
82119
}
83120

0 commit comments

Comments
 (0)