62 lines
2.8 KiB
PowerShell
62 lines
2.8 KiB
PowerShell
$null = New-Module {
|
|
class AppAssociations {
|
|
static $defaultAssociationsSelector = "/DefaultAssociations";
|
|
static $associationSelector = "./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) {
|
|
$document = [AppAssociations]::GetAppAssociations();
|
|
$associations = $document.SelectSingleNode([AppAssociations]::defaultAssociationsSelector);
|
|
|
|
[System.Xml.XmlNode] $association = $null;
|
|
$candidates = $associations.SelectNodes([AppAssociations]::GetSelector($identifier));
|
|
|
|
if ($candidates.Count -eq 1) {
|
|
$association = $candidates[0];
|
|
} else {
|
|
$association = ($associations.SelectNodes([AppAssociations]::associationSelector) | Select-Object -Last 1).CloneNode($true);
|
|
$association.Identifier = $identifier;
|
|
$association = $associations.AppendChild($association);
|
|
}
|
|
|
|
$association.ProgId = $progId;
|
|
$association.ApplicationName = $applicationName;
|
|
[AppAssociations]::SaveAssociations($document);
|
|
}
|
|
|
|
static SaveAssociations([xml] $document) {
|
|
$associations = $document.SelectSingleNode([AppAssociations]::defaultAssociationsSelector);
|
|
$defaultAssociations = $associations.SelectNodes([AppAssociations]::associationSelector);
|
|
$defaultAssociations | ForEach-Object { $associations.RemoveChild($_); } | Sort-Object -Property "Identifier" | ForEach-Object { $associations.AppendChild($_); };
|
|
|
|
$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);
|
|
$document.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);
|
|
}
|
|
}
|