forked from rgl/windows-vagrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libvirt-fix-cpu-driver.ps1
134 lines (119 loc) · 4.83 KB
/
libvirt-fix-cpu-driver.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
param(
[switch]$RunningAsScheduledTask = $false
)
# this is a fix for https://bugzilla.redhat.com/show_bug.cgi?id=1377155#c12
Set-StrictMode -Version Latest
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
trap {
Write-Host
Write-Host "ERROR: $_"
($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1' | Write-Host
($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1' | Write-Host
Exit 1
}
# NB this fix only applies to qemu.
if ('SeaBIOS' -ne (Get-CimInstance -ClassName WIN32_BIOS -Property Manufacturer).Manufacturer) {
Exit 0
}
#
# enable TLS 1.2.
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol `
-bor [Net.SecurityProtocolType]::Tls12
$taskName = 'libvirt-fix-cpu-driver'
$transcriptPath = "C:\tmp\$taskName.log"
$devConPath = 'C:\tmp\devcon.exe'
function Get-Devices($filter='*') {
# example devcon output:
#
# SWD\PRINTENUM\{385C45DF-7B30-4EB4-886E-7EFDD3817A40}
# Name: Microsoft Print to PDF
# Driver is running.
# {6FDE7547-1B65-48AE-B628-80BE62016026}\VIOSERIALPORT\4&176259CF&0&01
# Name: vport0p1
# Driver is running.
# 55 matching device(s) found.
$devices = @()
&$devConPath status $filter | ForEach-Object {$device=$null} {
# detect the start of a new device block.
if ($_ -match '^[^\d][^\\]+\\.+') {
$device = New-Object PSObject -Property @{
Id = $_
Name = ''
State = ''
}
$devices += $device
return
}
# detect the driver name field.
if ($_ -match '^\s+Name: (.+)') {
$device.Name = $matches[1].Trim()
return
}
# detect the driver state field.
if ($_ -match '^\s+.+') {
$device.State = $_.Trim()
return
}
}
return $devices
}
function Get-HidButtonDevice {
Get-Devices 'ACPI\ACPI0010*' | Where-Object {$_.Name -eq 'HID Button over Interrupt Driver'}
}
if ($RunningAsScheduledTask) {
Start-Transcript $transcriptPath
$device = Get-HidButtonDevice
if ($device) {
Write-Output "Removing the $($device.Name) ($($device.Id)) device..."
&$devConPath remove "@$($device.Id)"
# NB touching these registry keys requires us to be running as SYSTEM.
Remove-Item (Resolve-Path HKLM:\SYSTEM\DriverDataBase\DriverPackages\hidinterrupt.inf_amd64_*\Descriptors\ACPI\ACPI0010)
Remove-ItemProperty -Path HKLM:\SYSTEM\DriverDatabase\DeviceIds\ACPI\ACPI0010 -Name hidinterrupt.inf
Write-Output 'Rescanning the devices...'
&$devConPath rescan
}
} else {
if (!(Test-Path $devConPath)) {
$archiveUrl = 'https://github.com/rgl/devcon/releases/download/20181014/devcon.zip'
$archiveHash = '64b3380743722c7e72efbd63d35dd5fe4427ee852462299aa334437f244d7ea3'
$archiveName = Split-Path -Leaf $archiveUrl
$archivePath = "$env:TEMP\$archiveName"
Write-Host "Downloading $archiveName..."
(New-Object Net.WebClient).DownloadFile($archiveUrl, $archivePath)
$archiveActualHash = (Get-FileHash $archivePath -Algorithm SHA256).Hash
if ($archiveHash -ne $archiveActualHash) {
throw "$archiveName downloaded from $archiveUrl to $archivePath has $archiveActualHash hash witch does not match the expected $archiveHash"
}
Write-Host "Extracting $archiveName..."
Expand-Archive $archivePath (Split-Path -Parent $devConPath)
}
if (Get-HidButtonDevice) {
Write-Output 'Registering Scheduled Task...'
$action = New-ScheduledTaskAction `
-Execute 'PowerShell.exe' `
-Argument "-NoProfile -ExecutionPolicy Bypass $PSCommandPath -RunningAsScheduledTask"
Register-ScheduledTask `
-TaskName $taskName `
-Action $action `
-User 'SYSTEM' `
| Out-Null
Start-ScheduledTask `
-TaskName $taskName
Write-Output 'Waiting for the Scheduled Task to complete...'
while ((Get-ScheduledTask -TaskName $taskName).State -ne 'Ready') {
Start-Sleep -Seconds 1
}
$taskInfo = Get-ScheduledTaskInfo -TaskName $taskName
$taskResult = $taskInfo.LastTaskResult
Write-Output 'Unregistering Scheduled Task...'
Unregister-ScheduledTask `
-TaskName $taskName `
-Confirm:$false
Write-Output 'Scheduled Task output:'
Get-Content -ErrorAction SilentlyContinue $transcriptPath
Write-Output "Scheduled Task result: $taskResult"
Remove-Item $transcriptPath
}
Remove-Item $devConPath
}