PortValhalla/scripts/Windows/Scripts/AppAssociations.ps1

53 lines
2.2 KiB
PowerShell

$null = New-Module {
class AppAssociations {
static $associationSelector = "/DefaultAssociations/Association";
static [string] GetSelector([string] $identifier) {
return "$([AppAssociations]::associationSelector)[@Identifier='$identifier']";
}
static [xml] GetAppAssociations() {
[xml] $associations = [xml]::new();
$associations.LoadXml(((DISM /Online /Get-DefaultAppAssociations) | Select-Object -Skip 6 | Select-Object -SkipLast 2 | Out-String));
return $associations;
}
static SetAssociation([string] $identifier, [string] $progId, [string] $applicationName) {
$associations = [AppAssociations]::GetAppAssociations();
[System.Xml.XmlNode] $association = $null;
$candidates = $associations.SelectNodes([AppAssociations]::GetSelector($identifier));
if ($candidates.Count -eq 1) {
$association = $candidates[0];
} else {
$defaultAssociations = $associations.SelectSingleNode("/DefaultAssociations");
$association = ($associations.SelectNodes([AppAssociations]::associationSelector) | Select-Object -Last 1).CloneNode($true);
$association.Identifier = $identifier;
$association = $defaultAssociations.AppendChild($association);
}
$association.ProgId = $progId;
$association.ApplicationName = $applicationName;
$configFile = New-TemporaryFile;
$writerSettings = [System.Xml.XmlWriterSettings]::new();
$writerSettings.Indent = $true;
$writerSettings.Encoding = [System.Text.UTF8Encoding]::new();
$writer = [System.Xml.XmlWriter]::Create($configFile.FullName, $writerSettings);
$associations.Save($writer);
$writer.Dispose();
DISM /Online "/Import-DefaultAppAssociations:$($configFile.FullName)";
Remove-Item $configFile;
}
}
function Set-DefaultAppAssociation {
param (
[string] $Identifier,
[string] $ProgId,
[string] $ApplicationName
)
[AppAssociations]::SetAssociation($Identifier, $ProgId, $ApplicationName);
}
}