-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlibvirtualhid-driver-common.ps1
More file actions
109 lines (96 loc) · 3.44 KB
/
Copy pathlibvirtualhid-driver-common.ps1
File metadata and controls
109 lines (96 loc) · 3.44 KB
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
function Start-LibVirtualHidTranscript {
[CmdletBinding(SupportsShouldProcess)]
param([string] $Path)
if (-not $Path) {
return
}
try {
$logDirectory = Split-Path -Parent $Path
if ($logDirectory) {
New-Item -ItemType Directory -Path $logDirectory -Force | Out-Null
}
if ($PSCmdlet.ShouldProcess($Path, "Start libvirtualhid driver transcript")) {
Start-Transcript -Path $Path -Append | Out-Null
$script:LibVirtualHidTranscriptStarted = $true
}
} catch {
Write-Warning "Unable to start libvirtualhid driver transcript: $($_.Exception.Message)"
}
}
function Stop-LibVirtualHidTranscript {
[CmdletBinding(SupportsShouldProcess)]
param()
if (-not $script:LibVirtualHidTranscriptStarted) {
return
}
try {
if ($PSCmdlet.ShouldProcess("libvirtualhid driver transcript", "Stop transcript")) {
Stop-Transcript | Out-Null
}
} catch {
Write-Warning "Unable to stop libvirtualhid driver transcript: $($_.Exception.Message)"
}
}
function Get-LibVirtualHidRootDeviceInstanceId {
param([string] $TargetHardwareId)
try {
$devices = & pnputil.exe /enum-devices /deviceid $TargetHardwareId /deviceids
if ($LASTEXITCODE -eq 0) {
$instanceIds = @($devices |
Where-Object { $_ -match "^\s*Instance ID\s*:\s*(.+)$" } |
ForEach-Object { $Matches[1].Trim() })
if ($instanceIds.Count -gt 0) {
return $instanceIds
}
}
} catch {
Write-Verbose "Unable to enumerate PnP devices with pnputil: $($_.Exception.Message)"
}
try {
$prefix = "$TargetHardwareId\"
@(Get-CimInstance -ClassName Win32_PnPEntity -ErrorAction Stop |
Where-Object { $_.PNPDeviceID -like "$prefix*" -or $_.HardwareID -contains $TargetHardwareId } |
ForEach-Object { $_.PNPDeviceID })
} catch {
Write-Verbose "Unable to enumerate PnP devices: $($_.Exception.Message)"
@()
}
}
function Get-LibVirtualHidRegistryRootDevice {
param([string] $TargetHardwareId)
$rootKey = "HKLM:\SYSTEM\CurrentControlSet\Enum\ROOT"
Get-ChildItem -LiteralPath $rootKey -ErrorAction SilentlyContinue | ForEach-Object {
$rootDeviceId = $_.PSChildName
Get-ChildItem -LiteralPath $_.PSPath -ErrorAction SilentlyContinue | ForEach-Object {
$instanceId = "ROOT\$rootDeviceId\$($_.PSChildName)"
$hardwareIds = @()
try {
$hardwareIds = @((Get-ItemProperty -LiteralPath $_.PSPath -Name HardwareID -ErrorAction Stop).HardwareID)
} catch {
$hardwareIds = @()
}
$hasExactHardwareId = $hardwareIds -contains $TargetHardwareId
$hasCorruptHardwareId = (
$hardwareIds.Count -gt 1 -and
-not $hasExactHardwareId -and
(($hardwareIds -join "") -ieq $TargetHardwareId)
)
$hasTargetInstanceId = $instanceId -like "$TargetHardwareId\*"
if ($hasExactHardwareId -or $hasCorruptHardwareId -or $hasTargetInstanceId) {
$classGuid = $null
try {
$deviceProperties = Get-ItemProperty -LiteralPath $_.PSPath -ErrorAction Stop
$classGuid = $deviceProperties.ClassGUID
} catch {
Write-Verbose "Unable to read registry properties for $instanceId`: $($_.Exception.Message)"
}
[pscustomobject]@{
InstanceId = $instanceId
HasExactHardwareId = $hasExactHardwareId
HasCorruptHardwareId = $hasCorruptHardwareId
HasLegacyHidClass = $classGuid -ieq "{745a17a0-74d3-11d0-b6fe-00a0c90f57da}"
}
}
}
}
}