Refactor app association script

This commit is contained in:
Manuel Thalmann 2024-08-08 14:09:07 +02:00
parent b411a6a73d
commit e9677a3d6f

View file

@ -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;
}
}