Skip to content

Commit bdd3794

Browse files
fix sstandards v1
1 parent 7f74e80 commit bdd3794

File tree

4 files changed

+52
-231
lines changed

4 files changed

+52
-231
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function Convert-SingleStandardObject {
2+
3+
param(
4+
[Parameter(Mandatory = $true)]
5+
$Obj
6+
)
7+
$Obj = [pscustomobject]$Obj
8+
$AllActionValues = @()
9+
if ($Obj.PSObject.Properties.Name -contains 'combinedActions') {
10+
$AllActionValues = $Obj.combinedActions
11+
$null = $Obj.PSObject.Properties.Remove('combinedActions')
12+
} elseif ($Obj.PSObject.Properties.Name -contains 'action') {
13+
if ($Obj.action -and $Obj.action.value) {
14+
$AllActionValues = $Obj.action.value
15+
}
16+
$null = $Obj.PSObject.Properties.Remove('action')
17+
}
18+
19+
# Convert actions to booleans
20+
$Obj | Add-Member -NotePropertyName 'remediate' -NotePropertyValue ($AllActionValues -contains 'Remediate') -Force
21+
$Obj | Add-Member -NotePropertyName 'alert' -NotePropertyValue ($AllActionValues -contains 'warn') -Force
22+
$Obj | Add-Member -NotePropertyName 'report' -NotePropertyValue ($AllActionValues -contains 'Report') -Force
23+
24+
# Flatten "standards" if present
25+
if ($Obj.PSObject.Properties.Name -contains 'standards' -and $Obj.standards) {
26+
foreach ($standardKey in $Obj.standards.PSObject.Properties.Name) {
27+
$NestedStandard = $Obj.standards.$standardKey
28+
if ($NestedStandard) {
29+
foreach ($nsProp in $NestedStandard.PSObject.Properties) {
30+
$Obj | Add-Member -NotePropertyName $nsProp.Name -NotePropertyValue $nsProp.Value -Force
31+
}
32+
}
33+
}
34+
$null = $Obj.PSObject.Properties.Remove('standards')
35+
}
36+
37+
return $Obj
38+
}

Modules/CIPPCore/Public/Standards/ConvertTo-CippStandardObject.ps1

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,13 @@ function ConvertTo-CippStandardObject {
33
[Parameter(Mandatory = $true)]
44
$StandardObject
55
)
6-
7-
# If $StandardObject is an array (like for ConditionalAccessTemplate or IntuneTemplate),
8-
# we need to process each item individually.
96
if ($StandardObject -is [System.Collections.IEnumerable] -and -not ($StandardObject -is [string])) {
107
$ProcessedItems = New-Object System.Collections.ArrayList
118
foreach ($Item in $StandardObject) {
129
$ProcessedItems.Add((Convert-SingleStandardObject $Item)) | Out-Null
1310
}
1411
return [System.Collections.ArrayList]$ProcessedItems
1512
} else {
16-
# Single object scenario
1713
return Convert-SingleStandardObject $StandardObject
1814
}
1915
}
20-
21-
function Convert-SingleStandardObject {
22-
param(
23-
[Parameter(Mandatory = $true)]
24-
$Obj
25-
)
26-
27-
$Obj = [pscustomobject]$Obj
28-
29-
$AllActionValues = @()
30-
if ($Obj.PSObject.Properties.Name -contains 'combinedActions') {
31-
$AllActionValues = $Obj.combinedActions
32-
$null = $Obj.PSObject.Properties.Remove('combinedActions')
33-
} elseif ($Obj.PSObject.Properties.Name -contains 'action') {
34-
if ($Obj.action -and $Obj.action.value) {
35-
$AllActionValues = $Obj.action.value
36-
}
37-
$null = $Obj.PSObject.Properties.Remove('action')
38-
}
39-
40-
# Convert actions to booleans
41-
$Obj | Add-Member -NotePropertyName 'remediate' -NotePropertyValue ($AllActionValues -contains 'Remediate') -Force
42-
$Obj | Add-Member -NotePropertyName 'alert' -NotePropertyValue ($AllActionValues -contains 'warn') -Force
43-
$Obj | Add-Member -NotePropertyName 'report' -NotePropertyValue ($AllActionValues -contains 'Report') -Force
44-
45-
# Flatten standards if present
46-
if ($Obj.PSObject.Properties.Name -contains 'standards' -and $Obj.standards) {
47-
foreach ($standardKey in $Obj.standards.PSObject.Properties.Name) {
48-
$NestedStandard = $Obj.standards.$standardKey
49-
if ($NestedStandard) {
50-
foreach ($nsProp in $NestedStandard.PSObject.Properties) {
51-
$Obj | Add-Member -NotePropertyName $nsProp.Name -NotePropertyValue $nsProp.Value -Force
52-
}
53-
}
54-
}
55-
$null = $Obj.PSObject.Properties.Remove('standards')
56-
}
57-
58-
return $Obj
59-
}
Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +0,0 @@
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-
}
Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
1-
21
function Merge-CippStandards {
32
param(
4-
[Parameter(Mandatory = $true)] $Existing,
5-
[Parameter(Mandatory = $true)] $CurrentStandard
3+
[Parameter(Mandatory = $true)]
4+
[object]$Existing,
5+
[Parameter(Mandatory = $true)]
6+
[object]$New
67
)
7-
$Existing = [pscustomobject]$Existing
8-
$CurrentStandard = [pscustomobject]$CurrentStandard
9-
$ExistingActionValues = @()
10-
if ($Existing.PSObject.Properties.Name -contains 'action') {
11-
if ($Existing.action -and $Existing.action.value) {
12-
$ExistingActionValues = @($Existing.action.value)
13-
}
14-
$null = $Existing.PSObject.Properties.Remove('action')
15-
}
168

17-
$CurrentActionValues = @()
18-
if ($CurrentStandard.PSObject.Properties.Name -contains 'action') {
19-
if ($CurrentStandard.action -and $CurrentStandard.action.value) {
20-
$CurrentActionValues = @($CurrentStandard.action.value)
21-
}
22-
$null = $CurrentStandard.PSObject.Properties.Remove('action')
9+
if (-not $Existing) {
10+
return $New
2311
}
24-
$AllActionValues = ($ExistingActionValues + $CurrentActionValues) | Select-Object -Unique
25-
foreach ($prop in $CurrentStandard.PSObject.Properties) {
26-
if ($prop.Name -eq 'action') { continue }
27-
$Existing | Add-Member -NotePropertyName $prop.Name -NotePropertyValue $prop.Value -Force
12+
$ExistingIsArray = ($Existing -is [System.Collections.IEnumerable] -and -not ($Existing -is [string]))
13+
$NewIsArray = ($New -is [System.Collections.IEnumerable] -and -not ($New -is [string]))
14+
15+
if (-not $ExistingIsArray) {
16+
$Existing = @($Existing)
2817
}
29-
if ($AllActionValues.Count -gt 0) {
30-
$Existing | Add-Member -NotePropertyName 'combinedActions' -NotePropertyValue $AllActionValues -Force
18+
if (-not $NewIsArray) {
19+
$New = @($New)
3120
}
32-
33-
return $Existing
21+
return $Existing + $New
3422
}

0 commit comments

Comments
 (0)