Skip to content

Commit c2f637e

Browse files
committed
fix ninja one standards list
1 parent 416cbe3 commit c2f637e

File tree

2 files changed

+99
-79
lines changed

2 files changed

+99
-79
lines changed

Modules/CIPPCore/Public/Standards/Get-CIPPStandards.ps1

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ function Get-CIPPStandards {
2020
$Table = Get-CippTable -tablename 'templates'
2121
$Filter = "PartitionKey eq 'StandardsTemplateV2'"
2222
$Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter | Sort-Object TimeStamp).JSON |
23-
ForEach-Object {
24-
try {
25-
# Fix old "Action" => "action"
26-
$JSON = $_ -replace '"Action":', '"action":' -replace '"permissionlevel":', '"permissionLevel":'
27-
ConvertFrom-Json -InputObject $JSON -ErrorAction SilentlyContinue
28-
} catch {}
29-
} |
30-
Where-Object {
31-
$_.GUID -like $TemplateId -and $_.runManually -eq $runManually
32-
}
23+
ForEach-Object {
24+
try {
25+
# Fix old "Action" => "action"
26+
$JSON = $_ -replace '"Action":', '"action":' -replace '"permissionlevel":', '"permissionLevel":'
27+
ConvertFrom-Json -InputObject $JSON -ErrorAction SilentlyContinue
28+
} catch {}
29+
} |
30+
Where-Object {
31+
$_.GUID -like $TemplateId -and $_.runManually -eq $runManually
32+
}
3333

3434
# 1.5. Expand templates that contain TemplateList-Tags into multiple standards
3535
$ExpandedTemplates = foreach ($Template in $Templates) {
36+
Write-Information "Template $($Template.templateName) ($($Template.GUID)) processing..."
3637
$NewTemplate = $Template.PSObject.Copy()
3738
$ExpandedStandards = [ordered]@{}
3839
$HasExpansions = $false
@@ -42,8 +43,7 @@ function Get-CIPPStandards {
4243
$IsArray = $StandardValue -is [System.Collections.IEnumerable] -and -not ($StandardValue -is [string])
4344

4445
if ($IsArray) {
45-
$NewArray = @()
46-
foreach ($Item in $StandardValue) {
46+
$NewArray = foreach ($Item in $StandardValue) {
4747
if ($Item.'TemplateList-Tags'.value) {
4848
$HasExpansions = $true
4949
$Table = Get-CippTable -tablename 'templates'
@@ -54,13 +54,15 @@ function Get-CIPPStandards {
5454
$NewItem = $Item.PSObject.Copy()
5555
$NewItem.PSObject.Properties.Remove('TemplateList-Tags')
5656
$NewItem | Add-Member -NotePropertyName TemplateList -NotePropertyValue ([pscustomobject]@{
57-
label = "$($TemplateItem.RowKey)"
58-
value = "$($TemplateItem.RowKey)"
59-
}) -Force
60-
$NewArray = $NewArray + $NewItem
57+
label = "$($TemplateItem.RowKey)"
58+
value = "$($TemplateItem.RowKey)"
59+
}) -Force
60+
$NewItem | Add-Member -NotePropertyName TemplateId -NotePropertyValue $Template.GUID -Force
61+
$NewItem
6162
}
6263
} else {
63-
$NewArray = $NewArray + $Item
64+
$Item | Add-Member -NotePropertyName TemplateId -NotePropertyValue $Template.GUID -Force
65+
$Item
6466
}
6567
}
6668
$ExpandedStandards[$StandardName] = $NewArray
@@ -71,18 +73,19 @@ function Get-CIPPStandards {
7173
$Filter = "PartitionKey eq 'IntuneTemplate'"
7274
$TemplatesList = Get-CIPPAzDataTableEntity @Table -Filter $Filter | Where-Object -Property package -EQ $StandardValue.'TemplateList-Tags'.value
7375

74-
$NewArray = @()
75-
foreach ($TemplateItem in $TemplatesList) {
76+
$NewArray = foreach ($TemplateItem in $TemplatesList) {
7677
$NewItem = $StandardValue.PSObject.Copy()
7778
$NewItem.PSObject.Properties.Remove('TemplateList-Tags')
7879
$NewItem | Add-Member -NotePropertyName TemplateList -NotePropertyValue ([pscustomobject]@{
79-
label = "$($TemplateItem.RowKey)"
80-
value = "$($TemplateItem.RowKey)"
81-
}) -Force
82-
$NewArray = $NewArray + $NewItem
80+
label = "$($TemplateItem.RowKey)"
81+
value = "$($TemplateItem.RowKey)"
82+
}) -Force
83+
$NewItem | Add-Member -NotePropertyName TemplateId -NotePropertyValue $Template.GUID -Force
84+
$NewItem
8385
}
8486
$ExpandedStandards[$StandardName] = $NewArray
8587
} else {
88+
$StandardValue | Add-Member -NotePropertyName TemplateId -NotePropertyValue $Template.GUID -Force
8689
$ExpandedStandards[$StandardName] = $StandardValue
8790
}
8891
}
@@ -191,6 +194,13 @@ function Get-CIPPStandards {
191194
foreach ($Standard in $ComputedStandards.Keys) {
192195
$TempCopy = $ComputedStandards[$Standard].PSObject.Copy()
193196

197+
# Preserve TemplateId(s) before removing them from the Settings
198+
$PreservedTemplateIds = if ($TempCopy -is [System.Collections.IEnumerable] -and -not ($TempCopy -is [string])) {
199+
$TempCopy | ForEach-Object { $_.TemplateId }
200+
} else {
201+
$TempCopy.TemplateId
202+
}
203+
194204
# Remove 'TemplateId' from final output
195205
if ($TempCopy -is [System.Collections.IEnumerable] -and -not ($TempCopy -is [string])) {
196206
foreach ($subItem in $TempCopy) {
@@ -206,12 +216,7 @@ function Get-CIPPStandards {
206216
Tenant = 'AllTenants'
207217
Standard = $Standard
208218
Settings = $Normalized
209-
TemplateId = if ($ComputedStandards[$Standard] -is [System.Collections.IEnumerable] -and -not ($ComputedStandards[$Standard] -is [string])) {
210-
# If multiple items from multiple templates, you may have multiple TemplateIds
211-
$ComputedStandards[$Standard] | ForEach-Object { $_.TemplateId }
212-
} else {
213-
$ComputedStandards[$Standard].TemplateId
214-
}
219+
TemplateId = $PreservedTemplateIds
215220
}
216221
}
217222
} else {
@@ -416,6 +421,14 @@ function Get-CIPPStandards {
416421
# 4c. Output each final standard for this tenant
417422
foreach ($Standard in $ComputedStandards.Keys) {
418423
$TempCopy = $ComputedStandards[$Standard].PSObject.Copy()
424+
425+
# Preserve TemplateId(s) before removing them from the Settings
426+
$PreservedTemplateIds = if ($TempCopy -is [System.Collections.IEnumerable] -and -not ($TempCopy -is [string])) {
427+
$TempCopy | ForEach-Object { $_.TemplateId }
428+
} else {
429+
$TempCopy.TemplateId
430+
}
431+
419432
# Remove local 'TemplateId' from final object(s)
420433
if ($TempCopy -is [System.Collections.IEnumerable] -and -not ($TempCopy -is [string])) {
421434
foreach ($subItem in $TempCopy) {
@@ -431,7 +444,7 @@ function Get-CIPPStandards {
431444
Tenant = $TenantName
432445
Standard = $Standard
433446
Settings = $Normalized
434-
TemplateId = $ComputedStandards[$Standard].TemplateId
447+
TemplateId = $PreservedTemplateIds
435448
}
436449
}
437450
}

Modules/CippExtensions/Public/NinjaOne/Invoke-NinjaOneTenantSync.ps1

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,67 +1843,74 @@ function Invoke-NinjaOneTenantSync {
18431843
$StandardTemplates = Get-CIPPAzDataTableEntity @Templates | Where-Object { $_.PartitionKey -eq 'StandardsTemplateV2' }
18441844

18451845
$ParsedStandards = foreach ($Standard in $AppliedStandards) {
1846-
try {
1847-
$Template = ($StandardTemplates | Where-Object { $_.RowKey -eq $Standard.TemplateId }).JSON | ConvertFrom-Json
1848-
$StandardInfo = $StandardsDefinitions | Where-Object { ($_.name -replace 'standards.', '') -eq $Standard.Standard }
1849-
$StandardLabel = $StandardInfo.label
1850-
$ParsedActions = foreach ($Action in $Standard.Settings.PSObject.Properties) {
1851-
if ($Action.Value -eq $true -and $Action.Name -in @('remediate', 'report', 'alert')) {
1852-
(Get-Culture).TextInfo.ToTitleCase($Action.Name)
1846+
Write-Information "Processing Standard: $($Standard | ConvertTo-Json -Depth 10)"
1847+
if ($Standard.TemplateId.Count -gt 1) {
1848+
$TemplateListTemplates = foreach ($TemplateId in $Standard.TemplateId) {
1849+
if ($TemplateId) {
1850+
($StandardTemplates | Where-Object { $_.RowKey -eq $TemplateId }).JSON | ConvertFrom-Json
18531851
}
18541852
}
1853+
} else {
1854+
$Template = ($StandardTemplates | Where-Object { $_.RowKey -eq $Standard.TemplateId }).JSON | ConvertFrom-Json
1855+
}
1856+
$StandardInfo = $StandardsDefinitions | Where-Object { ($_.name -replace 'standards.', '') -eq $Standard.Standard }
1857+
$StandardLabel = $StandardInfo.label
1858+
$ParsedActions = foreach ($Action in $Standard.Settings.PSObject.Properties) {
1859+
if ($Action.Value -eq $true -and $Action.Name -in @('remediate', 'report', 'alert')) {
1860+
(Get-Culture).TextInfo.ToTitleCase($Action.Name)
1861+
}
1862+
}
18551863

1856-
# Handle template-based standards that have lists of templates
1857-
if ($Standard.Standard -in @('IntuneTemplate', 'ConditionalAccessTemplate', 'GroupTemplate')) {
1858-
# For template standards, create separate entries for each template
1859-
foreach ($Property in $Standard.Settings.PSObject.Properties) {
1860-
if ($Property.Value -is [Array]) {
1861-
foreach ($TemplateItem in $Property.Value) {
1862-
$TemplateName = $null
1863-
$TemplateActions = @()
1864-
1865-
Write-Information "Processing Template Item: $($TemplateItem | ConvertTo-Json -Depth 10)"
1866-
# Get template name
1867-
if ($TemplateItem.TemplateList.label) {
1868-
$TemplateName = $TemplateItem.TemplateList.label
1869-
} elseif ($TemplateItem.'TemplateList-Tags'.label) {
1870-
$TemplateName = $TemplateItem.'TemplateList-Tags'.label
1871-
} else {
1872-
$TemplateName = $TemplateItem.TemplateList.displayName
1873-
}
1864+
# Handle template-based standards that have lists of templates
1865+
if ($Standard.Standard -in @('IntuneTemplate', 'ConditionalAccessTemplate', 'GroupTemplate')) {
1866+
# For template standards, create separate entries for each template
1867+
foreach ($Property in $Standard.Settings.PSObject.Properties) {
1868+
if ($Property.Value -is [Array]) {
1869+
$x = 0
1870+
foreach ($TemplateItem in $Property.Value) {
1871+
$TemplateName = $null
1872+
$TemplateActions = @()
1873+
1874+
Write-Information "Processing Template Item: $($TemplateItem | ConvertTo-Json -Depth 10)"
1875+
# Get template name
1876+
if ($TemplateItem.TemplateList.label) {
1877+
$TemplateName = $TemplateItem.TemplateList.label
1878+
} elseif ($TemplateItem.'TemplateList-Tags'.label) {
1879+
$TemplateName = $TemplateItem.'TemplateList-Tags'.label
1880+
} else {
1881+
$TemplateName = $TemplateItem.TemplateList.displayName
1882+
}
18741883

1875-
# Get template-specific actions
1876-
$TemplateActions = foreach ($ItemAction in $TemplateItem.PSObject.Properties) {
1877-
if ($ItemAction.Value -eq $true -and $ItemAction.Name -in @('remediate', 'report', 'alert')) {
1878-
(Get-Culture).TextInfo.ToTitleCase($ItemAction.Name)
1879-
}
1884+
# Get template-specific actions
1885+
foreach ($ItemAction in $TemplateItem.PSObject.Properties) {
1886+
if ($ItemAction.Value -eq $true -and $ItemAction.Name -in @('remediate', 'report', 'alert')) {
1887+
$TemplateActions += (Get-Culture).TextInfo.ToTitleCase($ItemAction.Name)
18801888
}
1889+
}
18811890

1882-
# If no template-specific actions, use standard-level actions
1883-
if ($TemplateActions.Count -eq 0) {
1884-
$TemplateActions = $ParsedActions
1885-
}
1891+
# If no template-specific actions, use standard-level actions
1892+
if ($TemplateActions.Count -eq 0) {
1893+
$TemplateActions = $ParsedActions
1894+
}
18861895

1887-
if ($TemplateName) {
1888-
[PSCustomObject]@{
1889-
Standard = "$StandardLabel - $TemplateName"
1890-
Template = $Template.templateName
1891-
Actions = $TemplateActions -join ', '
1892-
}
1896+
if ($TemplateName) {
1897+
[PSCustomObject]@{
1898+
Standard = "$StandardLabel - $TemplateName"
1899+
Template = $TemplateListTemplates[$x].templateName
1900+
Actions = $TemplateActions -join ', '
18931901
}
18941902
}
1903+
$x++
18951904
}
18961905
}
1897-
} else {
1898-
# For non-template standards, use the original logic
1899-
[PSCustomObject]@{
1900-
Standard = $StandardLabel
1901-
Template = $Template.templateName
1902-
Actions = $ParsedActions -join ', '
1903-
}
19041906
}
1905-
} catch {
1906-
Write-Information "Error processing standard $($Standard ): $_"
1907+
} else {
1908+
# For non-template standards, use the original logic
1909+
[PSCustomObject]@{
1910+
Standard = $StandardLabel
1911+
Template = $Template.templateName
1912+
Actions = $ParsedActions -join ', '
1913+
}
19071914
}
19081915
}
19091916
$ParsedStandardsHTML = $ParsedStandards | ConvertTo-Html -As Table -Fragment

0 commit comments

Comments
 (0)