|
1 | | -function Get-CIPPStandards { |
2 | | - param( |
3 | | - [Parameter(Mandatory = $false)] |
4 | | - [string]$TenantFilter = 'allTenants', |
5 | | - [Parameter(Mandatory = $false)] |
6 | | - [switch]$ListAllTenants, |
7 | | - [Parameter(Mandatory = $false)] |
8 | | - $TemplateId = '*', |
9 | | - [Parameter(Mandatory = $false)] |
10 | | - $runManually = $false |
11 | | - ) |
12 | | - |
13 | | - $Table = Get-CippTable -tablename 'templates' |
14 | | - $Filter = "PartitionKey eq 'StandardsTemplateV2'" |
15 | | - $Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter | Sort-Object TimeStamp).JSON | ForEach-Object { |
16 | | - try { |
17 | | - $JSON = ($_).replace('"Action":', '"action":') #fix cap mistake of antique standards |
18 | | - ConvertFrom-Json -InputObject $JSON -ErrorAction SilentlyContinue |
19 | | - } catch { |
20 | | - } |
21 | | - } | Where-Object { |
22 | | - $_.GUID -like $TemplateId -and $_.runManually -eq $runManually |
23 | | - } |
24 | | - |
25 | | - $AllTenantsList = Get-Tenants |
26 | | - if ($TenantFilter -ne 'allTenants') { |
27 | | - $AllTenantsList = $AllTenantsList | Where-Object { |
28 | | - $_.defaultDomainName -eq $TenantFilter -or $_.customerId -eq $TenantFilter |
29 | | - } |
30 | | - } |
31 | | - |
32 | | - if ($ListAllTenants.IsPresent) { |
33 | | - $AllTenantsTemplates = $Templates | Where-Object { |
34 | | - $_.tenantFilter.value -contains 'AllTenants' |
35 | | - } |
36 | | - |
37 | | - $ComputedStandards = [ordered]@{} |
38 | | - |
39 | | - foreach ($Template in $AllTenantsTemplates) { |
40 | | - $Standards = $Template.standards |
41 | | - foreach ($StandardName in $Standards.PSObject.Properties.Name) { |
42 | | - $CurrentStandard = $Standards.$StandardName.PSObject.Copy() |
43 | | - $CurrentStandard | Add-Member -NotePropertyName 'TemplateId' -NotePropertyValue $Template.GUID -Force |
44 | | - |
45 | | - $Actions = $CurrentStandard.action.value |
46 | | - if ($Actions -contains 'Remediate' -or $Actions -contains 'warn' -or $Actions -contains 'Report') { |
47 | | - if (-not $ComputedStandards.Contains($StandardName)) { |
48 | | - $ComputedStandards[$StandardName] = $CurrentStandard |
49 | | - } else { |
50 | | - $MergedStandard = Merge-CippStandards $ComputedStandards[$StandardName] $CurrentStandard |
51 | | - $MergedStandard.TemplateId = $CurrentStandard.TemplateId |
52 | | - $ComputedStandards[$StandardName] = $MergedStandard |
53 | | - } |
54 | | - } |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - foreach ($Standard in $ComputedStandards.Keys) { |
59 | | - $TempCopy = $ComputedStandards[$Standard].PSObject.Copy() |
60 | | - $TempCopy.TemplateId ? $TempCopy.PSObject.Properties.Remove('TemplateId') : $null |
61 | | - |
62 | | - $Normalized = ConvertTo-CippStandardObject $TempCopy |
63 | | - |
64 | | - [pscustomobject]@{ |
65 | | - Tenant = 'AllTenants' |
66 | | - Standard = $Standard |
67 | | - Settings = $Normalized |
68 | | - TemplateId = $ComputedStandards[$Standard].TemplateId |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - } else { |
73 | | - foreach ($Tenant in $AllTenantsList) { |
74 | | - $TenantName = $Tenant.defaultDomainName |
75 | | - |
76 | | - $ApplicableTemplates = $Templates | ForEach-Object { |
77 | | - $template = $_ |
78 | | - $tenantFilterValues = $template.tenantFilter | ForEach-Object { $_.value } |
79 | | - $excludedTenantValues = @() |
80 | | - if ($template.excludedTenants) { |
81 | | - $excludedTenantValues = $template.excludedTenants | ForEach-Object { $_.value } |
82 | | - } |
83 | | - |
84 | | - $AllTenantsApplicable = $false |
85 | | - $TenantSpecificApplicable = $false |
86 | | - |
87 | | - if ($tenantFilterValues -contains 'AllTenants' -and (-not ($excludedTenantValues -contains $TenantName))) { |
88 | | - $AllTenantsApplicable = $true |
89 | | - } |
90 | | - if ($tenantFilterValues -contains $TenantName) { |
91 | | - $TenantSpecificApplicable = $true |
92 | | - } |
93 | | - |
94 | | - if ($AllTenantsApplicable -or $TenantSpecificApplicable) { |
95 | | - $template |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - $AllTenantTemplatesSet = $ApplicableTemplates | Where-Object { |
100 | | - $_.tenantFilter.value -contains 'AllTenants' |
101 | | - } |
102 | | - $TenantSpecificTemplatesSet = $ApplicableTemplates | Where-Object { |
103 | | - $_.tenantFilter.value -notcontains 'AllTenants' |
104 | | - } |
105 | | - |
106 | | - $ComputedStandards = [ordered]@{} |
107 | | - |
108 | | - foreach ($Template in $AllTenantTemplatesSet) { |
109 | | - $Standards = $Template.standards |
110 | | - foreach ($StandardName in $Standards.PSObject.Properties.Name) { |
111 | | - $CurrentStandard = $Standards.$StandardName.PSObject.Copy() |
112 | | - $CurrentStandard | Add-Member -NotePropertyName 'TemplateId' -NotePropertyValue $Template.GUID -Force |
113 | | - |
114 | | - $Actions = $CurrentStandard.action.value |
115 | | - if ($Actions -contains 'Remediate' -or $Actions -contains 'warn' -or $Actions -contains 'Report') { |
116 | | - if (-not $ComputedStandards.Contains($StandardName)) { |
117 | | - $ComputedStandards[$StandardName] = $CurrentStandard |
118 | | - } else { |
119 | | - $MergedStandard = Merge-CippStandards $ComputedStandards[$StandardName] $CurrentStandard |
120 | | - $MergedStandard | Add-Member -NotePropertyName 'TemplateId' -NotePropertyValue $CurrentStandard.TemplateId -Force |
121 | | - $ComputedStandards[$StandardName] = $MergedStandard |
122 | | - } |
123 | | - } |
124 | | - } |
125 | | - } |
126 | | - |
127 | | - foreach ($Template in $TenantSpecificTemplatesSet) { |
128 | | - $Standards = $Template.standards |
129 | | - foreach ($StandardName in $Standards.PSObject.Properties.Name) { |
130 | | - $CurrentStandard = $Standards.$StandardName.PSObject.Copy() |
131 | | - $CurrentStandard | Add-Member -NotePropertyName 'TemplateId' -NotePropertyValue $Template.GUID -Force |
132 | | - |
133 | | - $Actions = $CurrentStandard.action.value | Where-Object { $_ -in 'Remediate', 'warn', 'report' } |
134 | | - if ($Actions -contains 'Remediate' -or $Actions -contains 'warn' -or $Actions -contains 'Report') { |
135 | | - if (-not $ComputedStandards.Contains($StandardName)) { |
136 | | - $ComputedStandards[$StandardName] = $CurrentStandard |
137 | | - } else { |
138 | | - $MergedStandard = Merge-CippStandards $ComputedStandards[$StandardName] $CurrentStandard |
139 | | - $MergedStandard.TemplateId = $CurrentStandard.TemplateId |
140 | | - $ComputedStandards[$StandardName] = $MergedStandard |
141 | | - } |
142 | | - } |
143 | | - } |
144 | | - } |
145 | | - |
146 | | - foreach ($Standard in $ComputedStandards.Keys) { |
147 | | - $TempCopy = $ComputedStandards[$Standard].PSObject.Copy() |
148 | | - $TempCopy.PSObject.Properties.Remove('TemplateId') |
149 | | - |
150 | | - $Normalized = ConvertTo-CippStandardObject $TempCopy |
151 | | - |
152 | | - [pscustomobject]@{ |
153 | | - Tenant = $TenantName |
154 | | - Standard = $Standard |
155 | | - Settings = $Normalized |
156 | | - TemplateId = $ComputedStandards[$Standard].TemplateId |
157 | | - } |
158 | | - } |
159 | | - } |
160 | | - } |
161 | | -} |
0 commit comments