diff --git a/scripts/Windows/Scripts/AppAssociations.ps1 b/scripts/Windows/Scripts/AppAssociations.ps1 new file mode 100644 index 00000000..664b4275 --- /dev/null +++ b/scripts/Windows/Scripts/AppAssociations.ps1 @@ -0,0 +1,41 @@ +New-Module { + class AppAssociations { + static $associationSelector = "/DefaultAssociations/Association"; + + static [string] GetSelector([string] $identifier) { + return "$([AppAssociations]::associationSelector)[Identifier='$identifier']"; + } + + static [xml] GetAppAssociations() { + [xml] $associations = (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 { + $association = ($associations.SelectNodes([AppAssociations]::associationSelector) | Select-Object -Last 1)[0]; + $association.Identifier = $identifier; + $association = $associations.AppendChild($association); + } + + $association.ProgId = $progId; + $association.ApplicationName = $applicationName; + } + } + + function Set-Association { + param ( + [string] $Identifier, + [string] $ProgId, + [string] $ApplicationName + ) + + [AppAssociations]::SetAssociation($Identifier, $ProgId, $ApplicationName); + } +}