param( [Parameter(Mandatory=$true)][string]$Name, [Parameter(Mandatory=$true)][string]$ManifestPath, [switch]$Remove ) $ErrorActionPreference = 'Stop' if ($Name -notmatch '^com\.youtubedl\.test_[a-f0-9]{16}$') { throw 'Only isolated test hosts are allowed.' } $taskManifest = [IO.Path]::GetFullPath($ManifestPath) $taskKey = "Registry::HKEY_CURRENT_USER\Software\Microsoft\Edge\NativeMessagingHosts\$Name" if (Test-Path -LiteralPath $taskKey) { $taskRegistered = (Get-Item -LiteralPath $taskKey).GetValue('') if ($taskRegistered -and $taskRegistered -ne $taskManifest) { throw 'Test registration belongs to another path.' } } if ($Remove) { if (Test-Path -LiteralPath $taskKey) { Remove-Item -LiteralPath $taskKey } } else { $taskData = Get-Content -LiteralPath $taskManifest -Raw | ConvertFrom-Json if ($taskData.name -ne $Name) { throw 'Wrong test host manifest.' } New-Item -Path $taskKey -Force | Out-Null Set-Item -LiteralPath $taskKey -Value $taskManifest if ((Get-Item -LiteralPath $taskKey).GetValue('') -ne $taskManifest) { throw 'Test registration was not written.' } }