如何在 Windows 上使用 PowerShell 获取蓝牙设备电池百分比?

Mah*_*kar 3 windows powershell wmi battery bluetooth

我正在尝试绘制蓝牙耳机电池放电图表。为此,我需要读取所连接设备的电池百分比。我可以在设备的 GUI 上看到电源信息。有没有办法使用 PowerShell 获取连接的蓝牙设备的电池百分比信息?(比如使用 wmi 或其他任何东西)

小智 6

根据我的发现,您可以使用 Get-PnpDevice cmdlet 获取有关蓝牙设备的信息。这应该返回 PnP 设备及其状态、类、FriendlyName 和 InstanceID 的列表。

Get-PnpDevice
Run Code Online (Sandbox Code Playgroud)

您可以使用 -Class 参数过滤结果。要指定蓝牙 PnP 设备,您可以输入“Bluetooth”作为 -Class 参数的字符串值。

Get-PnpDevice -Class 'Bluetooth'
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用 -FriendlyName 参数通过其FriendlyName 从此列表中指定您想要的设备,并输入所需设备的FriendlyName 作为参数的字符串值。

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName'
Run Code Online (Sandbox Code Playgroud)

注意:您还可以使用 -InstanceId 参数指定设备,并将设备的 InstanceId 作为参数的字符串值提供。

如果随后将上一个命令通过管道传递给 Get-PnpDeviceProperty cmdlet,它将返回设备属性的列表,包括其 InstanceId、KeyName、Type 和 Data。

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
    Get-PnpDeviceProperty
Run Code Online (Sandbox Code Playgroud)

除此之外,我还可以通过使用 -KeyName 参数并输入(我假设)包含设备电源数据的属性的 KeyName 作为参数的字符串值来进一步过滤命令的结果。

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
    Get-PnpDeviceProperty -KeyName 'PropertyKeyName'
Run Code Online (Sandbox Code Playgroud)

不幸的是,这就是我所能解决的问题。希望我的贡献有所帮助。

  • 很好的解释和解决方案。我有一个 *MX Anywhere 2 鼠标*,查找“KeyName”有点棘手,因为它只是一个键字符串(不是友好名称)。将蓝牙面板的电量信息与这些值进行比较后,我找到了与电池百分比相对应的值。在我的例子中,最终为“(Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'MX Anywhere 2' | Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2').Data”。谢谢! (2认同)