@@ -14,10 +14,13 @@ param(
1414
1515 [string ] $RemoveCertificateSubject ,
1616
17+ [string ] $LogPath ,
18+
1719 [switch ] $Force
1820)
1921
2022$ErrorActionPreference = " Stop"
23+ $script :LibVirtualHidTranscriptStarted = $false
2124. (Join-Path $PSScriptRoot " libvirtualhid-driver-common.ps1" )
2225
2326function Invoke-CheckedCommand {
@@ -125,6 +128,31 @@ function Assert-PublishedName {
125128 }
126129}
127130
131+ function Remove-LibVirtualHidDeviceInstance {
132+ [CmdletBinding (SupportsShouldProcess )]
133+ param (
134+ [Parameter (Mandatory = $true )]
135+ [string ] $InstanceId
136+ )
137+
138+ if (-not $PSCmdlet.ShouldProcess ($InstanceId , " Remove libvirtualhid development device with pnputil" )) {
139+ return
140+ }
141+
142+ $output = @ (& pnputil.exe / remove-device $InstanceId 2>&1 )
143+ $exitCode = $LASTEXITCODE
144+ foreach ($line in $output ) {
145+ Write-Information ([string ] $line ) - InformationAction Continue
146+ }
147+
148+ if ($exitCode -ne 0 ) {
149+ Write-Warning (
150+ " pnputil.exe /remove-device $InstanceId exited with code $exitCode . " +
151+ " Continuing with forced driver-package removal; final state verification will still fail if cleanup is incomplete."
152+ )
153+ }
154+ }
155+
128156function Assert-LibVirtualHidRemoved {
129157 param (
130158 [string ] $TargetOriginalName ,
@@ -173,56 +201,60 @@ function Remove-DriverCertificate {
173201 }
174202}
175203
176- $publishedNames = @ ()
177- if ($PublishedName ) {
178- Assert-PublishedName - Name $PublishedName
179- $publishedNames += $PublishedName
180- } else {
181- try {
182- $publishedNames = @ (
183- Find-PublishedName `
184- - TargetOriginalName $OriginalName `
185- - TargetHardwareId $HardwareId
186- )
187- } catch {
188- throw " Unable to discover the staged libvirtualhid driver package through Windows APIs: $ ( $_.Exception.Message ) "
204+ Start-LibVirtualHidTranscript - Path $LogPath
205+
206+ try {
207+ $publishedNames = @ ()
208+ if ($PublishedName ) {
209+ Assert-PublishedName - Name $PublishedName
210+ $publishedNames += $PublishedName
211+ } else {
212+ try {
213+ $publishedNames = @ (
214+ Find-PublishedName `
215+ - TargetOriginalName $OriginalName `
216+ - TargetHardwareId $HardwareId
217+ )
218+ } catch {
219+ throw " Unable to discover the staged libvirtualhid driver package through Windows APIs: $ ( $_.Exception.Message ) "
220+ }
189221 }
190- }
191222
192- Remove-LibVirtualHidBrokerService - Name $BrokerServiceName
223+ Remove-LibVirtualHidBrokerService - Name $BrokerServiceName
193224
194- $deviceInstanceIds = @ (
195- Get-LibVirtualHidRootDeviceInstanceId - TargetHardwareId $HardwareId
196- Get-LibVirtualHidRegistryRootDevice - TargetHardwareId $HardwareId |
197- Select-Object - ExpandProperty InstanceId
198- ) | Select-Object - Unique
225+ $deviceInstanceIds = @ (
226+ Get-LibVirtualHidRootDeviceInstanceId - TargetHardwareId $HardwareId
227+ Get-LibVirtualHidRegistryRootDevice - TargetHardwareId $HardwareId |
228+ Select-Object - ExpandProperty InstanceId
229+ ) | Select-Object - Unique
199230
200- foreach ($instanceId in $deviceInstanceIds ) {
201- if ($PSCmdlet.ShouldProcess ($instanceId , " Remove libvirtualhid development device with pnputil" )) {
202- Invoke-CheckedCommand - FilePath " pnputil.exe" - Arguments @ (" /remove-device" , $instanceId )
231+ foreach ($instanceId in $deviceInstanceIds ) {
232+ Remove-LibVirtualHidDeviceInstance - InstanceId $instanceId
203233 }
204- }
205234
206- if ($publishedNames.Count -eq 0 ) {
207- Write-Warning " No staged libvirtualhid driver package matching $OriginalName was found."
208- } else {
209- foreach ($driverPackage in $publishedNames ) {
210- Assert-PublishedName - Name $driverPackage
211- $deleteArgs = @ (" /delete-driver" , $driverPackage , " /uninstall" )
212- if ($Force ) {
213- $deleteArgs += " /force"
214- }
235+ if ($publishedNames.Count -eq 0 ) {
236+ Write-Warning " No staged libvirtualhid driver package matching $OriginalName was found."
237+ } else {
238+ foreach ($driverPackage in $publishedNames ) {
239+ Assert-PublishedName - Name $driverPackage
240+ $deleteArgs = @ (" /delete-driver" , $driverPackage , " /uninstall" )
241+ if ($Force ) {
242+ $deleteArgs += " /force"
243+ }
215244
216- if ($PSCmdlet.ShouldProcess ($driverPackage , " Delete libvirtualhid driver package" )) {
217- Invoke-CheckedCommand - FilePath " pnputil.exe" - Arguments $deleteArgs
245+ if ($PSCmdlet.ShouldProcess ($driverPackage , " Delete libvirtualhid driver package" )) {
246+ Invoke-CheckedCommand - FilePath " pnputil.exe" - Arguments $deleteArgs
247+ }
218248 }
219249 }
220- }
221250
222- if (-not $WhatIfPreference ) {
223- Assert-LibVirtualHidRemoved `
224- - TargetOriginalName $OriginalName `
225- - TargetHardwareId $HardwareId `
226- - ServiceName $BrokerServiceName
251+ if (-not $WhatIfPreference ) {
252+ Assert-LibVirtualHidRemoved `
253+ - TargetOriginalName $OriginalName `
254+ - TargetHardwareId $HardwareId `
255+ - ServiceName $BrokerServiceName
256+ }
257+ Remove-DriverCertificate - Subject $RemoveCertificateSubject
258+ } finally {
259+ Stop-LibVirtualHidTranscript
227260}
228- Remove-DriverCertificate - Subject $RemoveCertificateSubject
0 commit comments