PortValhalla/scripts/Windows/Scripts/AppAssociations.ps1

105 lines
3.1 KiB
PowerShell
Raw Normal View History

2024-08-07 16:29:20 +00:00
using namespace System.Xml;
2023-07-26 10:53:38 +00:00
$null = New-Module {
2024-08-07 16:29:20 +00:00
$rootSelector = "/DefaultAssociations";
$associationSelector = "./Association";
2024-08-07 16:29:20 +00:00
<#
.SYNOPSIS
Generates a selector for getting the association of the specified identifier.
.PARAMETER Identifier
The identifier to get the association of.
#>
$getSelector = {
param(
[string] $Identifier
)
[string] $filter = "";
2024-08-07 16:29:20 +00:00
if ($Identifier) {
$filter = "[@Identifier='$Identifier']";
}
2024-08-07 16:29:20 +00:00
"$rootSelector/$associationSelector$filter";
}
2024-08-07 16:29:20 +00:00
<#
.SYNOPSIS
Gets the default app associations of the running Windows system.
#>
function Get-DefaultAppAssociations {
[OutputType([xml])]
param()
[xml]::new().LoadXml(((DISM /Online /Get-DefaultAppAssociations) | Select-Object -Skip 6 | Select-Object -SkipLast 2 | Out-String));
}
2024-08-07 16:29:20 +00:00
<#
.SYNOPSIS
Sets a default app association.
2024-08-07 16:29:20 +00:00
.PARAMETER Identifier
The identifier of the association to set.
2024-08-07 16:29:20 +00:00
.PARAMETER ProgId
The ProgId to set the association to.
2024-08-07 16:29:20 +00:00
.PARAMETER ApplicationName
The ApplicationName to set the association to.
#>
2023-07-26 10:53:30 +00:00
function Set-DefaultAppAssociation {
2024-08-07 16:29:20 +00:00
param(
[string] $Identifier,
[string] $ProgId,
[string] $ApplicationName
)
2024-08-07 16:29:20 +00:00
[System.Xml.XmlNode] $association = $null;
$document = Get-DefaultAppAssociations;
$candidates = $document.SelectNodes((& $getSelector $Identifier));
if ($candidates.Count -eq 1) {
$association = $candidates[0];
} else {
$association = ([System.Xml.XmlNode]($document.SelectNodes((& $getSelector)) | Select-Object -Last 1)[0]).CloneNode($true);
$association.Identifier = $Identifier;
}
$association.ProgId = $ProgId;
$association.ApplicationName = $ApplicationName;
Save-DefaultAppAssociations $document;
}
<#
.SYNOPSIS
Saves the default app associations.
.PARAMETER Document
The xml document containing the declarations of the app associations.
#>
function Save-DefaultAppAssociations {
param(
[xml] $Document
)
$root = $document.SelectSingleNode($rootSelector);
$associations = $root.SelectNodes((& $getSelector));
# Reorder associations by their Identifier
$associations | ForEach-Object { $root.RemoveChild($_); } | Sort-Object -Property "Identifier" | ForEach-Object { $root.AppendChild($_); };
$configFile = New-TemporaryFile;
$writerSettings = [XmlWriterSettings]::new();
$writerSettings.Indent = $true;
$writerSettings.Encoding = [System.Text.UTF8Encoding]::new();
$writer = [XmlWriter]::Create($configFile.FullName, $writerSettings);
$document.Save($writer);
$writer.Dispose();
2024-08-08 02:36:35 +00:00
Write-Host "$configFile";
Read-Host "press enter";
# DISM /Online "/Import-DefaultAppAssociations:$($configFile.FullName)";
2024-08-07 16:29:20 +00:00
Remove-Item $configFile;
}
}