2023-07-26 10:53:38 +00:00
|
|
|
$null = New-Module {
|
2023-07-26 10:45:01 +00:00
|
|
|
class AppAssociations {
|
2023-07-26 16:12:18 +00:00
|
|
|
static $defaultAssociationsSelector = "/DefaultAssociations";
|
|
|
|
static $associationSelector = "./Association";
|
2023-07-26 10:45:01 +00:00
|
|
|
|
|
|
|
static [string] GetSelector([string] $identifier) {
|
2023-07-26 11:01:45 +00:00
|
|
|
return "$([AppAssociations]::associationSelector)[@Identifier='$identifier']";
|
2023-07-26 10:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static [xml] GetAppAssociations() {
|
2023-07-26 10:49:32 +00:00
|
|
|
[xml] $associations = [xml]::new();
|
|
|
|
$associations.LoadXml(((DISM /Online /Get-DefaultAppAssociations) | Select-Object -Skip 6 | Select-Object -SkipLast 2 | Out-String));
|
2023-07-26 10:45:01 +00:00
|
|
|
return $associations;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SetAssociation([string] $identifier, [string] $progId, [string] $applicationName) {
|
2023-07-26 16:12:18 +00:00
|
|
|
$document = [AppAssociations]::GetAppAssociations();
|
|
|
|
$associations = $document.SelectSingleNode([AppAssociations]::defaultAssociationsSelector);
|
|
|
|
|
2023-07-26 10:45:01 +00:00
|
|
|
[System.Xml.XmlNode] $association = $null;
|
|
|
|
$candidates = $associations.SelectNodes([AppAssociations]::GetSelector($identifier));
|
|
|
|
|
|
|
|
if ($candidates.Count -eq 1) {
|
|
|
|
$association = $candidates[0];
|
|
|
|
} else {
|
2023-07-26 13:35:22 +00:00
|
|
|
$association = ($associations.SelectNodes([AppAssociations]::associationSelector) | Select-Object -Last 1).CloneNode($true);
|
2023-07-26 10:45:01 +00:00
|
|
|
$association.Identifier = $identifier;
|
2023-07-26 16:12:18 +00:00
|
|
|
$association = $associations.AppendChild($association);
|
2023-07-26 10:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$association.ProgId = $progId;
|
|
|
|
$association.ApplicationName = $applicationName;
|
2023-07-26 16:12:18 +00:00
|
|
|
[AppAssociations]::SaveAssociations($document);
|
2023-07-26 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 16:12:18 +00:00
|
|
|
static SaveAssociations([xml] $document) {
|
|
|
|
$associations = $document.SelectSingleNode([AppAssociations]::defaultAssociationsSelector);
|
2023-07-26 15:21:19 +00:00
|
|
|
$defaultAssociations = $associations.SelectNodes([AppAssociations]::associationSelector);
|
|
|
|
$defaultAssociations | ForEach-Object { $associations.RemoveChild($_); } | Sort-Object -Property "Identifier" | ForEach-Object { $associations.AppendChild($_); };
|
2023-07-26 10:50:35 +00:00
|
|
|
|
|
|
|
$configFile = New-TemporaryFile;
|
2023-07-26 12:49:03 +00:00
|
|
|
$writerSettings = [System.Xml.XmlWriterSettings]::new();
|
|
|
|
$writerSettings.Indent = $true;
|
|
|
|
$writerSettings.Encoding = [System.Text.UTF8Encoding]::new();
|
|
|
|
$writer = [System.Xml.XmlWriter]::Create($configFile.FullName, $writerSettings);
|
2023-07-26 16:28:31 +00:00
|
|
|
$document.Save($writer);
|
2023-07-26 13:26:02 +00:00
|
|
|
$writer.Dispose();
|
2023-07-26 10:50:35 +00:00
|
|
|
DISM /Online "/Import-DefaultAppAssociations:$($configFile.FullName)";
|
|
|
|
Remove-Item $configFile;
|
2023-07-26 10:45:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 10:53:30 +00:00
|
|
|
function Set-DefaultAppAssociation {
|
2023-07-26 10:45:01 +00:00
|
|
|
param (
|
|
|
|
[string] $Identifier,
|
|
|
|
[string] $ProgId,
|
|
|
|
[string] $ApplicationName
|
|
|
|
)
|
|
|
|
|
|
|
|
[AppAssociations]::SetAssociation($Identifier, $ProgId, $ApplicationName);
|
|
|
|
}
|
|
|
|
}
|