Refactor app association script

This commit is contained in:
Manuel Thalmann 2024-08-08 14:09:07 +02:00
parent d1f60a12b0
commit 8911b7460b

View file

@ -1,8 +1,9 @@
using namespace System.Xml; using namespace System.Xml;
$null = New-Module { $null = New-Module {
$associationElementName = "Association";
$rootSelector = "/DefaultAssociations"; $rootSelector = "/DefaultAssociations";
$associationSelector = "./Association"; $associationSelector = "./$associationElementName";
<# <#
.SYNOPSIS .SYNOPSIS
@ -60,11 +61,17 @@ $null = New-Module {
[System.Xml.XmlNode] $association = $null; [System.Xml.XmlNode] $association = $null;
$document = Get-DefaultAppAssociations; $document = Get-DefaultAppAssociations;
$candidates = $document.SelectNodes((& $getSelector $Identifier)); $candidates = $document.SelectNodes((& $getSelector $Identifier));
Write-Host "Number of potential associations: $($candidates.Count)"
if ($candidates.Count -eq 1) { if ($candidates.Count -eq 1) {
$association = $candidates[0]; $association = $candidates[0];
} else { } 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; $association.Identifier = $Identifier;
} }
@ -89,7 +96,7 @@ $null = New-Module {
$associations = $root.SelectNodes((& $getSelector)); $associations = $root.SelectNodes((& $getSelector));
# Reorder associations by their Identifier # 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; $configFile = New-TemporaryFile;
$writerSettings = [XmlWriterSettings]::new(); $writerSettings = [XmlWriterSettings]::new();
@ -98,9 +105,7 @@ $null = New-Module {
$writer = [XmlWriter]::Create($configFile.FullName, $writerSettings); $writer = [XmlWriter]::Create($configFile.FullName, $writerSettings);
$document.Save($writer); $document.Save($writer);
$writer.Dispose(); $writer.Dispose();
Write-Host "$configFile"; DISM /Online "/Import-DefaultAppAssociations:$($configFile.FullName)";
Read-Host "press enter";
# DISM /Online "/Import-DefaultAppAssociations:$($configFile.FullName)";
Remove-Item $configFile; Remove-Item $configFile;
} }
} }