From 523311700dd5b01f318aee80fb6003c45a9c7d29 Mon Sep 17 00:00:00 2001 From: Tomas Kracmar Date: Tue, 24 Nov 2026 12:27:16 +0100 Subject: [PATCH] feat(upstream): port Win32 scripts, quality update policies, sorted JSON Ported from Micke-K/IntuneManagement 3.10.1.x-3.11.0 (MIT): - Win32 app install/uninstall scripts are exported (#ScriptInfo plus the script files next to the JSON) and recreated on import and copy. - New object type Quality Updates (Policies) (windowsQualityUpdatePolicies); the old type is titled "Quality Updates (Profiles)". - SortJsonProperties setting writes exported JSON with sorted properties for stable diffs; always on for headless export. - EndpointManager.psm1 three-way merged with upstream (page-size flags, app fixes). UI-only changes were not taken. --- Extensions/EndpointManager.psm1 | 204 ++++++++++++++++++++++-- Extensions/MSGraph.psm1 | 155 +++++++++++++++++- Headless/IntuneManagement.Headless.psm1 | 3 +- 3 files changed, 343 insertions(+), 19 deletions(-) diff --git a/Extensions/EndpointManager.psm1 b/Extensions/EndpointManager.psm1 index 3562835..b483f66 100644 --- a/Extensions/EndpointManager.psm1 +++ b/Extensions/EndpointManager.psm1 @@ -162,7 +162,8 @@ function Invoke-InitializeModule ImportExtension = { Add-ConditionalAccessImportExtensions @args } PreImportCommand = { Start-PreImportConditionalAccess @args } PostExportCommand = { Start-PostExportConditionalAccess @args } - ExpandAssignmentsList = $false + ExpandAssignmentsList = $false + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -176,7 +177,8 @@ function Invoke-InitializeModule Permissons=@("Agreement.ReadWrite.All") PreImportCommand = { Start-PreImportTermsOfUse @args } PostExportCommand = { Start-PostExportTermsOfUse @args } - GroupId = "ConditionalAccess" + GroupId = "ConditionalAccess" + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -188,6 +190,7 @@ function Invoke-InitializeModule ImportOrder = 50 GroupId = "ConditionalAccess" ExpandAssignmentsList = $false + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -207,6 +210,7 @@ function Invoke-InitializeModule Permissons=@("DeviceManagementConfiguration.ReadWrite.All") Dependencies = @("ReusableSettings") GroupId = "EndpointSecurity" + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -239,6 +243,7 @@ function Invoke-InitializeModule PostExportCommand = { Start-PostExportCompliancePolicies @args } PreUpdateCommand = { Start-PreUpdateCompliancePolicies @args } GroupId = "CompliancePolicies" + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -286,6 +291,7 @@ function Invoke-InitializeModule SkipRemoveProperties = @('Id') # Id is removed by PreImport. Required for default profile PropertiesToRemoveForUpdate = @('isDefaultProfile','disableClientTelemetry') GroupId = "TenantAdmin" + SupportsPageSize = $false }) <# @@ -313,6 +319,7 @@ function Invoke-InitializeModule GroupId = "Azure" SkipAddIDOnExport = $true ExpandAssignmentsList = $false + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -336,6 +343,7 @@ function Invoke-InitializeModule AssignmentsType = "enrollmentConfigurationAssignments" PropertiesToRemoveForUpdate = @('priority') GroupId = "WinEnrollment" + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -359,6 +367,7 @@ function Invoke-InitializeModule AssignmentsType = "enrollmentConfigurationAssignments" GroupId = "EnrollmentRestrictions" ViewProperties = @("displayName","platformType","description","Id") + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -374,6 +383,7 @@ function Invoke-InitializeModule SkipRemoveProperties = @('Id') GroupId = "WinEnrollment" Icon = "EnrollmentStatusPage" + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -450,6 +460,7 @@ function Invoke-InitializeModule PreImportAssignmentsCommand = { Start-PreImportAssignmentsTermsAndConditions @args } GroupId = "TenantAdmin" ExpandAssignmentsList = $false + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -470,6 +481,7 @@ function Invoke-InitializeModule Dependencies = @("Applications") GroupId = "AppProtection" ExpandAssignmentsList = $false + SupportsPageSize = $false }) # These are also included in the managedAppPolicies API @@ -489,6 +501,7 @@ function Invoke-InitializeModule Icon = "AppConfiguration" GroupId = "AppConfiguration" ExpandAssignmentsList = $false + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -521,8 +534,8 @@ function Invoke-InitializeModule ImportOrder = 60 Expand="categories,assignments" # ODataMetadata is set to minimal so assignments can't be autodetected ODataMetadata="minimal" # categories property not supported with ODataMetadata full - PostFileImportCommand = { Start-PostFileImportApplications @args } - PostCopyCommand = { Start-PostCopyApplications @args } + PostFileImportCommand = { Start-PostFileImportApplication @args } + PostCopyCommand = { Start-PostCopyApplication @args } PreUpdateCommand = { Start-PreUpdateApplication @args } PreImportCommand = { Start-PreImportCommandApplication @args } DetailExtension = { Add-DetailExtensionApplications @args } @@ -592,7 +605,7 @@ function Invoke-InitializeModule }) Add-ViewItem (New-Object PSObject -Property @{ - Title = "Quality Updates" + Title = "Quality Updates (Profiles)" Id = "QualityUpdates" ViewID = "IntuneGraphAPI" API = "/deviceManagement/windowsQualityUpdateProfiles" @@ -600,6 +613,18 @@ function Invoke-InitializeModule Icon = "UpdatePolicies" GroupId = "WinQualityUpdates" PropertiesToRemoveForUpdate = @('releaseDateDisplayName','deployableContentDisplayName') + SupportsPageSize = $false + }) + + Add-ViewItem (New-Object PSObject -Property @{ + Title = "Quality Updates (Policies)" + Id = "QualityUpdatePolicies" + ViewID = "IntuneGraphAPI" + API = "/deviceManagement/windowsQualityUpdatePolicies" + Permissons=@("DeviceManagementConfiguration.ReadWrite.All") + Icon = "UpdatePolicies" + GroupId = "WinQualityUpdates" + SupportsPageSize = $false }) # Locations are not FULLY supported @@ -654,7 +679,7 @@ function Invoke-InitializeModule ViewProperties = @("name","description","Id") Expand="Settings" Icon="DeviceConfiguration" - GroupId = "DeviceConfiguration" + GroupId = "DeviceConfiguration" }) Add-ViewItem (New-Object PSObject -Property @{ @@ -669,7 +694,8 @@ function Invoke-InitializeModule API = "/deviceManagement/hardwareConfigurations" Permissons=@("DeviceManagementConfiguration.ReadWrite.All") Icon="DeviceConfiguration" - GroupId = "DeviceConfiguration" + GroupId = "DeviceConfiguration" + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -799,6 +825,7 @@ function Invoke-InitializeModule PreDeleteCommand = { Start-PreDeleteADMXFiles @args } ViewProperties = @("fileName","status","Id") PropertiesToRemove = @("languageCodes","targetPrefix","targetNamespace","policyType","revision","status","uploadDateTime") + SupportsPageSize = $false }) <# @@ -842,6 +869,7 @@ function Invoke-InitializeModule ExpandAssignmentsList = $false Icon = "ConditionalAccess" GroupId = "EndpointSecurity" + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -857,6 +885,7 @@ function Invoke-InitializeModule ExpandAssignmentsList = $false Icon = "ConditionalAccess" GroupId = "EndpointSecurity" + SupportsPageSize = $false }) Add-ViewItem (New-Object PSObject -Property @{ @@ -897,6 +926,7 @@ function Invoke-InitializeModule QUERYLIST = "`$top=500" Permissons = @("DeviceManagementConfiguration.ReadWrite.All") GroupId = "DeviceConfiguration" + ExpandAssignmentsList = $false }) } @@ -1077,7 +1107,14 @@ function Set-EMViewPanel $global:btnLoadAllPages.add_click({ Write-Status "Loading $($global:curObjectType.Title) objects" [array]$graphObjects = Get-GraphObjects -property $global:curObjectType.ViewProperties -objectType $global:curObjectType -AllPages - $graphObjects | Where-Object { $_ -ne $null } | ForEach-Object { $global:dgObjects.ItemsSource.AddNewItem($_) | Out-Null } + if(-not $global:dgObjects.Columns) + { + Show-GraphObjects -FromGraphObjects $graphObjects + } + else + { + $graphObjects | Where-Object { $_ -ne $null } | ForEach-Object { $global:dgObjects.ItemsSource.AddNewItem($_) | Out-Null } + } $global:dgObjects.ItemsSource.CommitNew() Set-GraphPagesButtonStatus Invoke-FilterBoxChanged $global:txtFilter -ForceUpdate @@ -1087,7 +1124,14 @@ function Set-EMViewPanel $global:btnLoadNextPage.add_click({ Write-Status "Loading $($global:curObjectType.Title) objects" [array]$graphObjects = Get-GraphObjects -property $global:curObjectType.ViewProperties -objectType $global:curObjectType -SinglePage - $graphObjects | Where-Object { $_ -ne $null } | ForEach-Object { $global:dgObjects.ItemsSource.AddNewItem($_) | Out-Null } + if(-not $global:dgObjects.Columns) + { + Show-GraphObjects -FromGraphObjects $graphObjects + } + else + { + $graphObjects | Where-Object { $_ -ne $null } | ForEach-Object { $global:dgObjects.ItemsSource.AddNewItem($_) | Out-Null } + } $global:dgObjects.ItemsSource.CommitNew() Set-GraphPagesButtonStatus Invoke-FilterBoxChanged $global:txtFilter @@ -2333,7 +2377,7 @@ function local:Add-AppConfigurationTargets $appName, $appId, $appType = $targetedApp -split "[|][!][|]" if(-not $appName -or -not $appId) { - Write-Log "App Name and Id is missing in string: $appApp" 2 + Write-Log "App Name and Id is missing in string: $targetedApp" 2 continue } $tmpApps = (Invoke-GraphRequest -Url "/deviceAppManagement/mobileApps?`$filter=displayName eq '$appName'").value @@ -2383,15 +2427,16 @@ function Start-PreImportAssignmentsAppConfiguration #region Applications -function Start-PostCopyApplications +function Start-PostCopyApplication { param($objCopyFrom, $objNew, $objectType) Start-ImportApp $objNew + Start-AddInstallScripts $objNew $objCopyFrom Write-Status "" } -function Start-PostFileImportApplications +function Start-PostFileImportApplication { param($obj, $objectType, $file) @@ -2412,6 +2457,7 @@ function Start-PostFileImportApplications } Start-ImportApp $obj $tmpFilName + Start-AddInstallScripts $obj $tmpObj } function local:Start-ImportApp @@ -2487,6 +2533,84 @@ function local:Start-ImportApp } } +function local:Start-AddInstallScripts +{ + param($obj, $fromAppObj) + + if($fromAppObj -and ($fromAppObj.activeInstallScript."#ScriptInfo" -or $fromAppObj.activeUninstallScript."#ScriptInfo")) + { + Write-Log "Importing scripts for $($obj.displayName)" + + $scriptsAdded = $false + $jsonData = @{} + $jsonData."@odata.type" = "#microsoft.graph.win32LobApp" + $jsonData."committedContentVersion" = "1" + + foreach ($scriptType in @('activeInstallScript','activeUninstallScript')) { + $scriptInfo = $fromAppObj.$scriptType.'#ScriptInfo' + if (-not $scriptInfo) { continue } + + Write-Log "Add $($scriptType -replace '^active','') script: $($scriptInfo.displayName)" + + $json = [ordered]@{ + '@odata.type' = $scriptInfo.'@odata.type' + displayName = $scriptInfo.displayName + enforceSignatureCheck = $scriptInfo.enforceSignatureCheck + runAs32Bit = $scriptInfo.runAs32Bit + content = $scriptInfo.content + } | ConvertTo-Json -Depth 10 -Compress + + $scriptObject = Invoke-GraphRequest -Url "deviceAppManagement/mobileApps/$($obj.id)/microsoft.graph.win32LobApp/contentVersions/1/scripts" -Method POST -Content $json + + if ($scriptObject) { + $jsonData.$scriptType = @{ targetId = $scriptObject.Id } + $scriptsAdded = $true + } + } + + $i = 0 + while($true) + { + $scripts = Invoke-GraphRequest -Url "deviceAppManagement/mobileApps/$($obj.id)/microsoft.graph.win32LobApp/contentVersions/1/scripts" + if(-not $scripts) + { + Write-Log "Failed to retrieve scripts for app after adding. Skipping Install/Uninstall script config." 2 + return + } + + if(($scripts.value.state | Select -Unique) -eq "commitSuccess") + { + Write-Log "Scripts added successfully" + break + } + if($i -ge 12) + { + Write-Log "Install/Uninstall scripts are still not in pending state after waiting for 1 minute." 3 + return + } + + Write-Log "Waiting for scripts to be added..." + Start-Sleep -Seconds 5 + $i++ + } + + if($scriptsAdded) + { + Write-Log "Add script info to app" + $json = ConvertTo-Json $jsonData -Depth 10 + $status = Invoke-GraphRequest -Url "deviceAppManagement/mobileApps/$($obj.id)" -Method PATCH -Body $json + if($status -eq $true) + { + Write-Log "Install/Uninstall script info updated successfully" + } + else + { + Write-Log "Failed to update Install/Uninstall script info" 2 + } + } + } +} + function Start-PreUpdateApplication { param($obj, $objectType, $curObject, $fromObj) @@ -2524,6 +2648,9 @@ function Start-PreImportCommandApplication $obj.officeSuiteAppDefaultFileFormat = "officeOpenXMLFormat" } } + + if($obj.activeInstallScript) { $obj.activeInstallScript = $null } + if($obj.activeUninstallScript) { $obj.activeUninstallScript = $null } } function Add-DetailExtensionApplications @@ -2711,6 +2838,8 @@ function Start-PreImportAssignmentsApplications function Start-PreDeleteApplications { + param($obj, $objectType) + if($obj.'@odata.type' -eq "#microsoft.graph.microsoftStoreForBusinessApp") { # Don't delete Microsoft Store for Business Apps @@ -2748,6 +2877,16 @@ function Start-PostExportApplications } } } + + if($obj.activeInstallScript.'#ScriptInfo'.displayName) + { + [IO.File]::WriteAllBytes((Join-Path $path "$($fi.BaseName)_$($obj.activeInstallScript.'#ScriptInfo'.displayName)"), ([System.Convert]::FromBase64String($obj.activeInstallScript.'#ScriptInfo'.content))) + } + + if($obj.activeUninstallScript.'#ScriptInfo'.displayName) + { + [IO.File]::WriteAllBytes((Join-Path $path "$($fi.BaseName)_$($obj.activeUninstallScript.'#ScriptInfo'.displayName)"), ([System.Convert]::FromBase64String($obj.activeUninstallScript.'#ScriptInfo'.content))) + } } catch { @@ -2791,7 +2930,7 @@ function Start-PostExportApplications } else { - Write-Log "Cound not find encryption file" + Write-Log "Could not find encryption file" } } } @@ -2851,7 +2990,7 @@ function Add-ScriptExportApplications function Start-PostGetApplications { param($obj, $objectType) - if($obj.Object.dependentAppCount -is [Int] -and ($obj.Object.dependentAppCount -gt 0 -or $obj.Object.supersededAppCount -gt 0)) { + if(($obj.Object.dependentAppCount -as [int]) -gt 0 -or ($obj.Object.supersededAppCount -as [int]) -gt 0) { $relationships = (Invoke-GraphRequest -Url "/deviceAppManagement/mobileApps/$($obj.Id)/relationships?`$filter=targetType%20eq%20microsoft.graph.mobileAppRelationshipType%27child%27").value $dependencyApps = @() $supersededApps = @() @@ -2871,6 +3010,37 @@ function Start-PostGetApplications { $obj.Object | Add-Member -MemberType NoteProperty -Name "#CustomRefSupersedence" -Value ($supersededApps -join "|*|") } } + + if($obj.Object.'@odata.type' -eq "#microsoft.graph.win32LobApp") + { + if($obj.Object.activeInstallScript.targetId -or $obj.Object.activeUninstallScript.targetId) + { + $scriptInfo = (Invoke-GraphRequest -Url "/deviceAppManagement/mobileApps/$($obj.Id)/microsoft.graph.win32LobApp/contentVersions/$($obj.Object.committedContentVersion)/scripts/").value + + foreach($script in $scriptInfo) { + $scriptFullInfo = (Invoke-GraphRequest -Url "/deviceAppManagement/mobileApps/$($obj.Id)/microsoft.graph.win32LobApp/contentVersions/$($obj.Object.committedContentVersion)/scripts/$($script.id)?`$select=Id,Content") + if($scriptFullInfo.Content) + { + $script.content = $scriptFullInfo.Content + } + + if($obj.Object.activeInstallScript.targetId -eq $script.id) + { + $tpObject = $obj.Object.activeInstallScript + } + elseif($obj.Object.activeUninstallScript.targetId -eq $script.id) + { + $tpObject = $obj.Object.activeUninstallScript + } + else + { + Write-Log "Script with id $($script.id) is not referenced by active install or uninstall script. Skipping." 2 + continue + } + $tpObject | Add-Member -MemberType NoteProperty -Name "#ScriptInfo" -Value $script -Force + } + } + } } function Start-PostImportApplications @@ -2933,9 +3103,9 @@ function local:Add-ApplicationReferences Write-Log "No $appName application found with version $appVer" 2 continue } - elseif(-not ($tmpApp | measure).Count -gt 1) + elseif(($tmpApp | measure).Count -gt 1) { - Write-Log "Multiple $appName application found with version $appVer" 2 + Write-Log "Multiple $appName applications found with version $appVer" 2 continue } Write-Log "Add $appName ($appVer) to Dependency list" @@ -2954,7 +3124,7 @@ function local:Add-ApplicationReferences $appName, $appVer, $appId, $appType = $suppApp -split "[|][!][|]" if(-not $appName -or -not $appVer) { - Write-Log "Could not get Name and Version from string: $appApp" 2 + Write-Log "Could not get Name and Version from string: $suppApp" 2 continue } $tmpApps = (Invoke-GraphRequest -Url "/deviceAppManagement/mobileApps?`$filter=displayName eq '$appName'").value diff --git a/Extensions/MSGraph.psm1 b/Extensions/MSGraph.psm1 index d2f2024..05d4fc6 100644 --- a/Extensions/MSGraph.psm1 +++ b/Extensions/MSGraph.psm1 @@ -200,6 +200,14 @@ function Invoke-InitializeModule Description = "This will add object ID to the export file to support objects with the same name e.g. ObjectName_ObjectId.json" }) "ImportExport" + Add-SettingsObject (New-Object PSObject -Property @{ + Title = "Sort Json Properties" + Key = "SortJsonProperties" + Type = "Boolean" + DefaultValue = $false + Description = "Write exported JSON with sorted properties so every export has the same property order (clean diffs, stable change tracking)" + }) "ImportExport" + Add-SettingsObject (New-Object PSObject -Property @{ Title = "Use Batch API (Preview)" Key = "UseBatchAPI" @@ -4674,7 +4682,14 @@ function Save-GraphObjectToFile { param($obj, $fileName) - $json = $obj | ConvertTo-Json -Depth 50 + if((Get-SettingValue "SortJsonProperties") -eq $true) + { + $json = $obj | ConvertTo-JsonSortedGraph -Depth 50 + } + else + { + $json = $obj | ConvertTo-Json -Depth 50 + } if($global:Organization.Id) { @@ -4691,6 +4706,144 @@ function Save-GraphObjectToFile } } +function Invoke-SortJsonGraphObject { + param( + [Parameter(Mandatory)] + $InputObject + ) + + if ($null -eq $InputObject) { return $null } + + if ($InputObject -is [System.Collections.IList]) { + $new = @() + foreach ($item in $InputObject) { + $new += Invoke-SortJsonGraphObject $item + } + return ,$new + } + + if ($InputObject -is [System.Collections.IDictionary] -or + $InputObject -is [System.Management.Automation.PSCustomObject]) { + + $props = + if ($InputObject -is [System.Collections.IDictionary]) { + $InputObject.Keys + } else { + $InputObject.PSObject.Properties.Name + } + + $baseGroups = @{} + + foreach ($p in $props) { + if ($p -match '^(.+?)@(.+)$') { + $base = $matches[1] + if (-not $baseGroups.ContainsKey($base)) { + $baseGroups[$base] = @{ + Base = $null + Meta = New-Object System.Collections.ArrayList + } + } + [void]$baseGroups[$base].Meta.Add($p) + } + } + + foreach ($p in $props) { + if ($baseGroups.ContainsKey($p)) { + $baseGroups[$p].Base = $p + } + } + + $atProps = @() # starts with @ + $hashProps = @() # starts with # + $normal = @() # everything else + + foreach ($p in $props) { + + if ($p.StartsWith('@')) { + $atProps += $p + continue + } + + if ($p.StartsWith('#')) { + $hashProps += $p + continue + } + + # If in a baseGroup, skip for now (added later) + if ($baseGroups.ContainsKey($p)) { + continue + } + + $normal += $p + } + + $atProps = $atProps | Sort-Object + $normal = $normal | Sort-Object + $hashProps = $hashProps | Sort-Object + + $orderedNames = @() + + $orderedNames += $atProps + + foreach ($base in ($baseGroups.Keys | Sort-Object)) { + + $meta = $baseGroups[$base].Meta | Sort-Object + + foreach ($m in $meta) { + $orderedNames += $m + } + + if ($null -ne $InputObject.$base) { + $orderedNames += $base + } + } + + $orderedNames += $normal + $orderedNames += $hashProps + + $result = [ordered]@{} + foreach ($p in $orderedNames) { + $value = + if ($InputObject -is [System.Collections.IDictionary]) { + $InputObject[$p] + } else { + $InputObject.$p + } + + if($null -ne $value) { + $result[$p] = Invoke-SortJsonGraphObject $value + } + else { + $result[$p] = $null + } + + if($InputObject.$p -is [Array] -and $result[$p] -isnot [Array]) { + if($result[$p] -eq $null) { + $result[$p] = @() + } + else { + $result[$p] = @($result[$p]) + } + } + } + + return [pscustomobject]$result + } + + return $InputObject +} + +function ConvertTo-JsonSortedGraph { + param( + [Parameter(ValueFromPipeline, Mandatory)] + $InputObject, + + [int]$Depth = 50 + ) + $sorted = Invoke-SortJsonGraphObject $InputObject + return $sorted | ConvertTo-Json -Depth $Depth +} + function Get-GraphObjectFile { param($obj, $objectType, $path) diff --git a/Headless/IntuneManagement.Headless.psm1 b/Headless/IntuneManagement.Headless.psm1 index b2a82c1..04ffab0 100644 --- a/Headless/IntuneManagement.Headless.psm1 +++ b/Headless/IntuneManagement.Headless.psm1 @@ -40,6 +40,7 @@ function Get-DefaultIntunePolicyObjectTypes "UpdatePolicies", "FeatureUpdates", "QualityUpdates", + "QualityUpdatePolicies", "DriverUpdateProfiles", "HardwareConfigurations", "InventoryPolicies", @@ -306,7 +307,7 @@ function Export-IntunePolicies -BatchConfig $batchConfig ` -SettingsFile $SettingsFile ` -BatchFile $BatchFile ` - -SettingOverrides @{ UseBatchAPI = (-not $NoBatchApi) } + -SettingOverrides @{ UseBatchAPI = (-not $NoBatchApi); SortJsonProperties = $true } } function Import-IntunePolicies