Skip to content

Instantly share code, notes, and snippets.

@muedsa
Created September 22, 2023 09:54
Show Gist options
  • Save muedsa/d5a94e2d5827d3b518063b9a2d2fdda3 to your computer and use it in GitHub Desktop.
Save muedsa/d5a94e2d5827d3b518063b9a2d2fdda3 to your computer and use it in GitHub Desktop.
获取WMI中的蓝牙设备电量
# ATTENTION: Don't forget to allow the powershell script to run first.
# Here is a step by step of how to execute this script:
# - Open a Powershell command prompt.
# - Allow shel script execution with command:
# Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
# - Execute the current powershell script with the command:
# .\GetBatteryLevel_DOQAUS-CARE1v1.ps1
# - The results may take some time to come. A pop-up will show on your
# main screen. You must click 'Ok' to unblock the Powershell command prompt.
$StartTime = Get-Date
$BTDeviceFriendlyName = "WF-1000XM3"
$Shell = New-Object -ComObject "WScript.Shell"
$BTHDevices = Get-PnpDevice -FriendlyName "*$($BTDeviceFriendlyName)*"
if ($BTHDevices) {
$BatteryLevels = foreach ($Device in $BTHDevices) {
$BatteryProperty = Get-PnpDeviceProperty -InstanceId $Device.InstanceId -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' |
Where-Object { $_.Type -ne 'Empty' } |
Select-Object -ExpandProperty Data
if ($BatteryProperty) {
$BatteryProperty
}
}
if ($BatteryLevels) {
$ElapsedTime = (Get-Date) - $StartTime
$ElapsedTimeMilliseconds = [math]::Round($ElapsedTime.TotalMilliseconds, 0)
$ElapsedTimeStr = "$($ElapsedTimeMilliseconds) ms"
if ($ElapsedTimeMilliseconds -gt 1000) {
$ElapsedTimeSeconds = $ElapsedTime.TotalSeconds
$ElapsedTimeStr = "$($ElapsedTimeSeconds) sec"
}
$Message = "Battery Level of $($BTDeviceFriendlyName): $BatteryLevels %`nElapsed Time: $($ElapsedTimeStr)"
$Button = $Shell.Popup($Message, 0, "Battery Level", 0)
}
else {
Write-Host "No battery level information found for $($BTDeviceFriendlyName) devices."
}
}
else {
Write-Host "Bluetooth device found."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment