From 99e95420539cf9bb2eee63801c2a85f935dce591 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 8 Aug 2024 14:09:07 +0200 Subject: [PATCH] Refactor app association script --- scripts/Windows/Scripts/AppAssociations.ps1 | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/Windows/Scripts/AppAssociations.ps1 b/scripts/Windows/Scripts/AppAssociations.ps1 index 50479cd9..dae933e0 100644 --- a/scripts/Windows/Scripts/AppAssociations.ps1 +++ b/scripts/Windows/Scripts/AppAssociations.ps1 @@ -1,8 +1,9 @@ using namespace System.Xml; $null = New-Module { + $associationElementName = "Association"; $rootSelector = "/DefaultAssociations"; - $associationSelector = "./Association"; + $associationSelector = "./$associationElementName"; <# .SYNOPSIS @@ -60,11 +61,17 @@ $null = New-Module { [System.Xml.XmlNode] $association = $null; $document = Get-DefaultAppAssociations; $candidates = $document.SelectNodes((& $getSelector $Identifier)); + Write-Host "Number of potential associations: $($candidates.Count)" if ($candidates.Count -eq 1) { $association = $candidates[0]; } else { - $association = ([System.Xml.XmlNode]($document.SelectNodes((& $getSelector)) | Select-Object -Last 1)[0]).CloneNode($true); + $association = $document.SelectSingleNode($rootSelector).AppendChild($document.CreateElement($associationElementName)); + + foreach ($attributeName in @("Identifier", "ProgId", "ApplicationName")) { + $null = $association.Attributes.Append($document.CreateAttribute($attributeName)); + } + $association.Identifier = $Identifier; } @@ -89,7 +96,7 @@ $null = New-Module { $associations = $root.SelectNodes((& $getSelector)); # Reorder associations by their Identifier - $associations | ForEach-Object { $root.RemoveChild($_); } | Sort-Object -Property "Identifier" | ForEach-Object { $root.AppendChild($_); }; + $null = $associations | ForEach-Object { $root.RemoveChild($_) } | Sort-Object -Property "Identifier" | ForEach-Object { $root.AppendChild($_); }; $configFile = New-TemporaryFile; $writerSettings = [XmlWriterSettings]::new(); @@ -98,9 +105,7 @@ $null = New-Module { $writer = [XmlWriter]::Create($configFile.FullName, $writerSettings); $document.Save($writer); $writer.Dispose(); - Write-Host "$configFile"; - Read-Host "press enter"; - # DISM /Online "/Import-DefaultAppAssociations:$($configFile.FullName)"; + DISM /Online "/Import-DefaultAppAssociations:$($configFile.FullName)"; Remove-Item $configFile; } }