Reputation: 21
I'm using the Win32_PnPEntity class to get all the devices in a computer, but the Win32_PnPEntity class does not list the hidden devices. Hidden devices in the Windows device manager have a status "Currently, this hardware device is not connected to the computer. (Code 45)" and can be shown by clicking the menu option in Device Manager: View > Show hidden devices (Windows 10).
Does anyone know how to get the hidden devices?
Upvotes: 2
Views: 3397
Reputation: 21
For some reason unknown to me, it appears that Microsoft has hardcoded Win32_PnpEntity on the backend to return non-'OK' devices only when using Get-PnpDevice. You can emulate this behavior by setting a special CIM option called 'MI_OPERATIONOPTIONS_POWERSHELL_CMDLETNAME' to contain the value '-PnpDevice' (e.g. 'Get-PnpDevice'). This will work outside of PowerShell (i.e. in other languages) if you are using the CIM libraries/functions that support setting CIM options. I do not believe, however, there is any way to set this variable in a raw WMI query unfortunately.
$CimSession = New-CimSession
$Options = [Microsoft.Management.Infrastructure.Options.CimOperationOptions]::new()
$Options.SetOption('MI_OPERATIONOPTIONS_POWERSHELL_CMDLETNAME','XXX-PnpDevice')
$CimSession.EnumerateInstances('ROOT/CIMV2','Win32_PnpEntity',$Options) | Where-Object 'Status' -ne 'OK'
Upvotes: 2
Reputation: 41
You can do it using the command:
Get-PnpDevice -class "Ports"
Status Class FriendlyName
------ ----- ------------
OK Ports Communications Port (COM1)
Unknown Ports Silicon Labs Dual CP2105 USB to UART Bridge: Enhanced
Unknown Ports Arduino Uno (COM5)
Unknown Ports Silicon Labs Dual CP2105 USB to UART Bridge: Standard
OK Ports Prolific USB-to-Serial Comm Port (COM6)
Here you can see my COM ports that have been disconnected (status: unknown)
Upvotes: 4
Reputation: 1476
You can make use of ConfigManagerErrorCode
. Refer Win32_PnPEntity and Win32_PnPEntity MSDN. You have not mentioned if you are using powershell or C# for scripting, i am assuming powershell.
$result = @{Expression = {$_.Name}; Label = "Device Name"},
@{Expression = {$_.ConfigManagerErrorCode} ; Label = "Status Code" }
Get-WmiObject -Class Win32_PnpEntity -ComputerName localhost -Namespace Root\CIMV2 | Where-Object {$_.ConfigManagerErrorCode -gt 0 } | Format-Table $result
Upvotes: 0