Win32_PnPEntity not getting hidden devices - windows

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?

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)

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

Related

How to get Bluetooth device battery percentage using PowerShell on windows?

I am trying to plot a graph for Bluetooth headphone battery discharge. For that I need to read battery percentage of the connected device. I can see power information is available on GUI for the device. Is there any way to get the battery percentage info for connected Bluetooth device using PowerShell? (like using wmi or anything else)
In my findings, you can get information on Bluetooth devices using the Get-PnpDevice cmdlet. This should return a list of PnP Devices, their Status, Class, FriendlyName and InstanceID.
Get-PnpDevice
You can filter the results with -Class parameter. To specify Bluetooth PnP devices, you can enter "Bluetooth" as a string value for the -Class parameter.
Get-PnpDevice -Class 'Bluetooth'
You can then specify the device you have in mind from this list by their FriendlyName using the -FriendlyName parameter and enter the desired device's FriendlyName as a string value for the parameter.
Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName'
Note: You can also specify the device using the -InstanceId parameter and providing the device's InstanceId as a string value for the parameter.
If you then pipe the previous command to the Get-PnpDeviceProperty cmdlet, it will return a list of the device's properties, including its InstanceId, KeyName, Type and Data.
Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
Get-PnpDeviceProperty
Beyond this point, I was able to further filter the results of the command by using the -KeyName parameter and entering the KeyName of the property that (I assume) contains Device Power Data as a string value for the parameter.
Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
Get-PnpDeviceProperty -KeyName 'PropertyKeyName'
Unfortunately this is as far as I've gotten to solving the problem. Hopefully my contribution helps.
As far as I am aware, there is no way to poll bluetooth device data beyond what you would get with Get-WmiObject, since the battery status seen in Windows Settings -> Bluetooth Devices is something coming from the vendors/devices driver and seems to, as of now, be inaccessible by PowerShell, unless there is some exotic snapin I am not aware of.
You can get all possible device information via this command:
Get-WmiObject -Query "select * from win32_PnPEntity" | Where Name -like "MyDeviceName"
Or if you are unsure how the device is named as of now, this would return a complete list of "devices":
Get-WmiObject -Query "select * from win32_PnPEntity" | Select Name
Additionally, I couldn't find battery information in the registry - maybe someone more knowledge can expand on that because the registry probably contains the necessary information as it must be stored somewhere on the device.
Paulo Amaral's comment above is indeed the answer.
The code below will provide you with a command you can reuse to query the battery state of your device:
Get-PnpDevice -FriendlyName "*<Device Name>*" | ForEach-Object {
$local:test = $_ |
Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' |
Where Type -ne Empty;
if ($test) {
"To query battery for $($_.FriendlyName), run the following:"
" Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data"
""
"The result will look like this:";
Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data
}
}
For my <Headset> device, the output looked similar to:
To query battery for <Headset>, run the following:
Get-PnpDeviceProperty -InstanceId BTHENUM\{0000####-0000-####-####-############}_VID&########_PID&####\#&########&#&############_######### -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2'
The result will look like this:
90

How to list bluetooth devices near me using PowerShell

How to list all Bluetooth devices paired or currently near me and particullary MAC adress ? I want the equivalent of the command :
netsh wlan show network mode=bssid
I am able to list all Bluetooth devices and characteristics but not MAC adress with the command :
Get-PnpDevice | Where-Object {$_.Class -eq "Bluetooth"}
foreach ($device in $devices) { echo $device.InstanceId }
Note it is not a problem if I need to manually shrink results and if the list is not in a perfect layout.
The PowerShell command :
Get-ChildItem -Path HKLM:\SYSTEM\ControlSet001\Services\DeviceAssociationService\State\Store\ | Select-String -Pattern Bluetooth
will print devices already paired :
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DeviceAssociationService\State\Store\Bluetooth#BluetoothXX:XX:XX:XX:XX:b2-YY:YY:YY:YY:YY:YY
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DeviceAssociationService\State\Store\Bluetooth#BluetoothXX:XX:XX:XX:XX:b2-ZZ:ZZ:ZZ:ZZ:ZZ:ZZ
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\DeviceAssociationService\State\Store\BluetoothLE#BluetoothLEXX:XX:XX:XX:XX:b2-WW:WW:WW:WW:WW:WW
The XX:XX:XX:XX:XX values is your Bluetooth MAC adress.

Disable Exclusive Mode for all audio devices in Windows 10

I need to configure audio devices a large number of PCs.
I am stuck on how to Disable Exclusive Mode, "allow applications to take exclusive-mode of this device"
I can disable all the media devices:
Get-PnpDevice -class "MEDIA" | ForEach-Object {Disable-PnpDevice -InstanceId $_.InstanceID -Confirm:$false}
I can enable the desired devices:
Get-PnpDevice -Class Media | where FriendlyName -like "*NVIDIA High Definition Audio*" | where Status -eq "OK" | ForEach-Object { Disable-PnpDevice -instanceid $_.instanceid -Confirm:$false }
I can set the default audio device (playback/recording) leveraging AudioDeviceCmdlets.
I can even set the sound reduction to 0 through the registry
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Multimedia\Audio] "UserDuckingPreference"=dword:00000003
But I cannot find an API or a data structure that corresponds to where the state is changed and how?
Can someone point me in the right direction here?
These parameters are available in the registry key:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{xxx-xxx-xxx-xxx}\Properties
(change XXX by the GUID of your default record device)
The names of the keys are:
{b3f8fa53-0004-438e-9003-51a46e139bfc},3
{b3f8fa53-0004-438e-9003-51a46e139bfc},4
The value can be 0 or 1.
This correspond to the check box "Allow applications to take exclusive control of this device" and "Give exclusive mode applications priority"
But those key are owned by system account and cant be edited easily with a script.
If you are OK to use external program, a nirsoft sofware utility can do the job:
Nirsoft SoundVolumeView
You can use it from cli:
SoundVolumeView.exe /SetExclusivePriority "XXXXX" 0
SoundVolumeView.exe /SetAllowExclusive "XXXXXX" 0
(XXX can be ID or name of the record device)

how to get unidentified network adaptor name with batch

I already knew how to get all network adaptors names, but I can't tell the difference between normally using and not identified
By using
netsh interface ip show interfaces
or other commands ,you may get all adaptors names.
The necessary information can be obtained using the Win32_NetworkAdapter class.For example:
Get-CimInstance -ClassName Win32_NetworkAdapter -Filter #'
NetConnectionID = "Local Area Connection" AND
NetConnectionStatus=2 AND NetEnabled = True AND PhysicalAdapter = True
'# | Format-List *
The Get-WmiObject cmdlet can also be used, but starting in PowerShell 3.0, it has been superseded by Get-CimInstance.Starting with Windows 8, you can use the Get-NetAdapter cmdlet.

Reliably disabling internet access on Windows 7 and up

I'm trying to write a script that requires turning off the Internet entirely and then turning it back on. I'd like it to work in as many cases as possible...
support for Window 7 and up
if multiple Internet connections are on (like WiFi and LAN)
regardless of how these connections are named
limited user account, UAC?
ipconfig /release and ipconfig /renew, as suggested in this answer, do not work with 2 internet connections. /release disables the active connection (say WLAN) but the computer falls back on the LAN connection and you're still online.
$connectedAdapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2"
$connectedAdapters | Invoke-WmiMethod -Name disable
My questions are:
Is NetConnectionStatus = 2 a reliable proxy for internet access, and is it available on Windows 7 and up, regardless of the brand of your NIC?
Is this compatible with a limited user account with UAC on? I think so...
On my machine this query also catches VirtualBox Host-Only Ethernet Adapter. Is it a problem if I disable/enable it too?
Are Get-WmiObject and Invoke-WmiMethod are available on Windows 7 and up, right?
The questions are not entirely settled, but I've developed a script that works:
PowerShell Internet Connection helper functions: Go-Offline, Go-Online, Test-InternetAccess
#Requires -Version 2.0
function Test-InternetAccess {
<#
.SYNOPSIS
Tests connectivity by pinging Google DNS servers once
.DESCRIPTION
Uses Test-Connection to ping a host, with -Quiet for returning a boolean. The default is a highly available Google DNS server (8.8.4.4)
.EXAMPLE
Test-InternetAccess
.EXAMPLE
Test-InternetAccess example.com
.INPUTS
None.
.OUTPUTS
Boolean
#>
param (
[String]
$RemoteHost = "google-public-dns-b.google.com"
)
Test-Connection -Computer $RemoteHost -BufferSize 16 -Count 1 -Quiet
}
function Go-Offline {
<#
.SYNOPSIS
Disables your internet connection
.DESCRIPTION
Finds all network adapters that appear connected and disables them, taking you offline. Later on you can re-enable just those adapters, because they've been stored in an XML file. Connected adapters are detected through WMI. A NetConnectionStatus value of 2 means Connected. 7 means Media Disconnected.
.EXAMPLE
Go-Offline
.INPUTS
None.
.OUTPUTS
None.
.LINK
https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/07/use-powershell-to-identify-your-real-network-adapter/
.LINK
https://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param()
$XMLLocation = "$env:TEMP\Disabled-NICs.xml"
if (Test-InternetAccess) {
$connectedAdapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2"
# Go offline
$connectedAdapters | Invoke-WMIMethod -Name disable 1>$null
# Save which adapters were connected at the time
$connectedAdapters | Select Name, DeviceID | Export-Clixml -Path $XMLLocation -Force
Write-Output "You've been taken offline!"
Sleep 1
} else {
Write-Output "Connection already down..."
Sleep 1
}
}
function Go-Online {
<#
.SYNOPSIS
Re-enables your internet connection
.DESCRIPTION
Finds all network adapters that were previously disabled by Go-Offline and enables them. This information is persisted in a temp file.
.EXAMPLE
Go-Online
.INPUTS
None.
.OUTPUTS
None.
.LINK
https://blogs.technet.microsoft.com/heyscriptingguy/2011/10/07/use-powershell-to-identify-your-real-network-adapter/
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param()
$XMLLocation = "$env:TEMP\Disabled-NICs.xml"
if (!(Test-InternetAccess)) {
# Get the NICs that have been previously disabled
$connectedAdapters = Import-Clixml "$env:TEMP\Disabled-NICs.xml" | Select -ExpandProperty DeviceID | ForEach {Get-WMIObject -Class Win32_NetworkAdapter -Filter "DeviceID = $_"}
# Get back online
$connectedAdapters | Invoke-WMIMethod -Name enable | Out-Null
Write-Output "Internet access restored!" # Triggers early, before actual re-connection
Sleep 1
}
}
Tested on Windows 7.

Resources