PortValhalla/scripts/Windows/Scripts/AppAssociations.ps1

111 lines
3.3 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-08 12:09:07 +00:00
$associationElementName = "Association";
2024-08-07 16:29:20 +00:00
$rootSelector = "/DefaultAssociations";
2024-08-08 12:09:07 +00:00
$associationSelector = "./$associationElementName";
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()
2024-08-08 03:04:59 +00:00
$result = [xml]::new();
$result.LoadXml(((DISM /Online /Get-DefaultAppAssociations) | Select-Object -Skip 6 | Select-Object -SkipLast 2 | Out-String));
$result;
2024-08-07 16:29:20 +00:00
}
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 {
2024-08-08 12:09:07 +00:00
$association = $document.SelectSingleNode($rootSelector).AppendChild($document.CreateElement($associationElementName));
foreach ($attributeName in @("Identifier", "ProgId", "ApplicationName")) {
$null = $association.Attributes.Append($document.CreateAttribute($attributeName));
}
2024-08-07 16:29:20 +00:00
$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
2024-08-08 12:09:07 +00:00
$null = $associations | ForEach-Object { $root.RemoveChild($_) } | Sort-Object -Property "Identifier" | ForEach-Object { $root.AppendChild($_); };
2024-08-07 16:29:20 +00:00
$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-15 11:57:15 +00:00
$null = DISM /Online "/Import-DefaultAppAssociations:$($configFile.FullName)";
2024-08-07 16:29:20 +00:00
Remove-Item $configFile;
}
}