82 lines
5.1 KiB
PowerShell
82 lines
5.1 KiB
PowerShell
# Opt-in VirtualBox fixture workaround, never part of the RVBox client.
|
|
# Only the mouse-class VBoxMouse filter is changed; drivers are not uninstalled.
|
|
param([ValidateSet('Bypass', 'Restore', 'Status', 'Verify')][string]$Action = 'Status')
|
|
$ErrorActionPreference = 'Stop'
|
|
$ProgressPreference = 'SilentlyContinue'
|
|
$key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e96f-e325-11ce-bfc1-08002be10318}'
|
|
$backup = 'C:\ProgramData\RVBoxFixture\mouse-filter-backup.json'
|
|
$filters = [string[]](Get-ItemProperty -LiteralPath $key -Name UpperFilters).UpperFilters
|
|
$devices = @(Get-PnpDevice -Class Mouse -PresentOnly)
|
|
|
|
if ($Action -in @('Bypass', 'Restore')) {
|
|
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
|
|
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
throw 'Mouse-filter changes require a full-token administrator.'
|
|
}
|
|
if ($filters -notcontains 'mouclass') { throw 'Refusing to change a mouse class without mouclass.' }
|
|
if ((Get-Item -LiteralPath $key).GetValueKind('UpperFilters') -ne [Microsoft.Win32.RegistryValueKind]::MultiString) {
|
|
throw 'Expected REG_MULTI_SZ mouse-class UpperFilters.'
|
|
}
|
|
if ($Action -eq 'Bypass') {
|
|
if (-not ($devices | Where-Object { $_.InstanceId -like 'HID\VID_80EE&PID_0021\*' -and $_.Status -eq 'OK' })) {
|
|
throw 'Expected healthy VirtualBox USB-tablet mouse is absent.'
|
|
}
|
|
if (-not (Test-Path -LiteralPath $backup)) {
|
|
if ($filters -notcontains 'VBoxMouse') { throw 'No VBoxMouse filter and no original backup.' }
|
|
New-Item -ItemType Directory -Path (Split-Path -Parent $backup) -Force | Out-Null
|
|
$json = [ordered]@{ key = $key; upperFilters = $filters; savedUtc = [DateTime]::UtcNow.ToString('o') } | ConvertTo-Json
|
|
# Never replace the first backup, including on repeated runs.
|
|
$file = [IO.File]::Open($backup, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::None)
|
|
try {
|
|
$bytes = [Text.Encoding]::UTF8.GetBytes($json)
|
|
$file.Write($bytes, 0, $bytes.Length)
|
|
$file.Flush($true)
|
|
} finally { $file.Dispose() }
|
|
}
|
|
}
|
|
$saved = Get-Content -LiteralPath $backup -Raw | ConvertFrom-Json
|
|
$original = [string[]]$saved.upperFilters
|
|
if ($saved.key -ne $key -or $original -notcontains 'mouclass' -or $original -notcontains 'VBoxMouse') {
|
|
throw 'Backup does not match the expected mouse class.'
|
|
}
|
|
$bypassed = [string[]]@($original | Where-Object { $_ -ine 'VBoxMouse' })
|
|
if (($filters -join "`0") -cne ($original -join "`0") -and ($filters -join "`0") -cne ($bypassed -join "`0")) {
|
|
throw 'Mouse filters have changed since backup; refusing to overwrite other changes.'
|
|
}
|
|
$desired = if ($Action -eq 'Bypass') { $bypassed } else { $original }
|
|
New-ItemProperty -LiteralPath $key -Name UpperFilters -PropertyType MultiString -Value ([string[]]$desired) -Force | Out-Null
|
|
$actual = [string[]](Get-ItemProperty -LiteralPath $key -Name UpperFilters).UpperFilters
|
|
if (($actual -join "`0") -cne ($desired -join "`0")) { throw 'Filter update did not persist.' }
|
|
$rebootRequired = $false
|
|
foreach ($device in $devices) {
|
|
Write-Output "Restarting mouse device: $($device.InstanceId)"
|
|
& "$env:SystemRoot\System32\pnputil.exe" /restart-device $device.InstanceId
|
|
$deviceExit = $LASTEXITCODE
|
|
Write-Output "pnputil_exit=$deviceExit"
|
|
if ($deviceExit -eq 3010) { $rebootRequired = $true }
|
|
elseif ($deviceExit -ne 0) {
|
|
throw "Mouse-device restart failed ($deviceExit); backup remains at $backup. Inspect or restore before retrying."
|
|
}
|
|
}
|
|
# Registry success is not proof of a detached driver; no automatic reboot.
|
|
Write-Output "reboot_required=$rebootRequired"
|
|
}
|
|
Write-Output "backup=$backup"
|
|
Get-ItemProperty -LiteralPath $key | Select-Object UpperFilters, LowerFilters | Format-List
|
|
Get-PnpDevice -Class Mouse -PresentOnly | Select-Object Status, FriendlyName, InstanceId | Format-List
|
|
Get-Service VBoxMouse, mouclass, mouhid, VBoxService -ErrorAction SilentlyContinue | Select-Object Name, Status | Format-Table
|
|
if ($Action -eq 'Verify') {
|
|
if ($filters -contains 'VBoxMouse') { throw 'VBoxMouse remains in the mouse class filter.' }
|
|
$driver = Get-Service VBoxMouse -ErrorAction SilentlyContinue
|
|
if ($driver -and $driver.Status -ne 'Stopped') { throw 'VBoxMouse is still loaded; restart Windows normally before testing.' }
|
|
foreach ($name in @('mouclass', 'mouhid', 'VBoxService')) {
|
|
if ((Get-Service $name).Status -ne 'Running') { throw "Required standard driver/service is not running: $name" }
|
|
}
|
|
if ($filters -notcontains 'mouclass' -or ($devices | Where-Object { $_.Status -ne 'OK' }) -or
|
|
-not ($devices | Where-Object { $_.InstanceId -like 'HID\VID_80EE&PID_0021\*' -and $_.Status -eq 'OK' })) {
|
|
throw 'Standard mouse class or USB-tablet device is unhealthy.'
|
|
}
|
|
Write-Output 'mouse_bypass_verified=True'
|
|
}
|
|
Write-Output 'RVBOX_GUEST_OK'
|