74 lines
2.2 KiB
PowerShell
74 lines
2.2 KiB
PowerShell
param([switch]$Install)
|
||
|
||
$ErrorActionPreference = 'Stop'
|
||
$inf = Join-Path $PSScriptRoot 'qhsusb.inf'
|
||
$cer = Join-Path $PSScriptRoot 'qhsusb.cer'
|
||
|
||
function Test-QhsusbInstalled {
|
||
$files = @(Get-ChildItem 'C:\Windows\INF\oem*.inf' -ErrorAction SilentlyContinue)
|
||
foreach ($file in $files) {
|
||
try {
|
||
$text = Get-Content -LiteralPath $file.FullName -Raw -ErrorAction Stop
|
||
} catch {
|
||
continue
|
||
}
|
||
if ($text -match 'QHSUSB__BULK' -and $text -match 'VID_05C6&PID_9008') {
|
||
return $true
|
||
}
|
||
}
|
||
return $false
|
||
}
|
||
|
||
function Test-Admin {
|
||
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
||
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
|
||
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
||
}
|
||
|
||
if (-not $Install) {
|
||
if (Test-QhsusbInstalled) {
|
||
Write-Output "DRIVER`tREADY"
|
||
} else {
|
||
Write-Output "DRIVER`tMISSING"
|
||
}
|
||
exit 0
|
||
}
|
||
|
||
if (-not (Test-Admin)) {
|
||
$arg = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Install"
|
||
try {
|
||
Start-Process -FilePath powershell.exe -ArgumentList $arg -Verb RunAs -Wait | Out-Null
|
||
} catch {
|
||
Write-Output "DRIVER`tFAIL`tcancel"
|
||
exit 1
|
||
}
|
||
if (Test-QhsusbInstalled) {
|
||
Write-Output "DRIVER`tINSTALLED"
|
||
exit 0
|
||
}
|
||
Write-Output "DRIVER`tFAIL"
|
||
exit 1
|
||
}
|
||
|
||
if (Test-Path -LiteralPath $cer) {
|
||
# 这份目录是 libwdi 自签名。只放进 TrustedPublisher 时,
|
||
# 代码完整性仍报 0x800b0109,pnputil 最终返回 0xE0000247。
|
||
& certutil.exe -addstore -f Root $cer | Out-Null
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Output ("DRIVER`tFAIL`tcert-root-" + $LASTEXITCODE)
|
||
exit 1
|
||
}
|
||
& certutil.exe -addstore -f TrustedPublisher $cer | Out-Null
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Output ("DRIVER`tFAIL`tcert-publisher-" + $LASTEXITCODE)
|
||
exit 1
|
||
}
|
||
}
|
||
$proc = Start-Process -FilePath "$env:SystemRoot\System32\pnputil.exe" -ArgumentList @('/add-driver', $inf, '/install') -Wait -PassThru -NoNewWindow
|
||
if (Test-QhsusbInstalled) {
|
||
Write-Output "DRIVER`tINSTALLED"
|
||
exit 0
|
||
}
|
||
Write-Output ("DRIVER`tFAIL`t" + $proc.ExitCode)
|
||
exit 1
|