Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed potential null exception and code beauty adjustments in battery…
…_test_log.ps1
- Loading branch information
1 parent
084f155
commit 798972e
Showing
1 changed file
with
19 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
function WriteBatteryPercent ([int]$Append) { | ||
if ($Append -ne 0) { | ||
Write-Output (([string](Get-WmiObject Win32_Battery).EstimatedChargeRemaining) + ';' + (Get-Date -Format "%H:%m:s")) ` | ||
Write-Output (([string]::Format((Get-WmiObject Win32_Battery).EstimatedChargeRemaining)) + ';' + (Get-Date -Format "%H:%m:s")) ` | ||
| Tee-Object -Filepath "battery_test_log.txt" -Append; | ||
} else { | ||
Write-Output (([string](Get-WmiObject Win32_Battery).EstimatedChargeRemaining) + ';' + (Get-Date -Format "%H:%m:s")) ` | ||
Write-Output (([string]::Format((Get-WmiObject Win32_Battery).EstimatedChargeRemaining)) + ';' + (Get-Date -Format "%H:%m:s")) ` | ||
| Tee-Object -Filepath "battery_test_log.txt"; | ||
} | ||
} | ||
|
||
if (-not (Test-Path -LiteralPath "battery_test_log.txt" -PathType Leaf)) { | ||
Write-Output "battery percentage;time" | Tee-Object -Filepath "battery_test_log.txt"; | ||
WriteBatteryPercent -Append 0 -Sleep 10; | ||
} else { | ||
Write-Output "battery percentage;time"; | ||
} | ||
|
||
|
||
while ($true) { ` | ||
if ($BatteryPercent -ne (Get-WmiObject Win32_Battery).EstimatedChargeRemaining) { | ||
$BatteryPercent = (Get-WmiObject Win32_Battery).EstimatedChargeRemaining; | ||
WriteBatteryPercent -Append 1; | ||
while ($true) { | ||
$battery=(Get-WmiObject Win32_Battery); | ||
if($battery -ne $null) { | ||
$CurrentPercent=$battery.EstimatedChargeRemaining; | ||
if( $BatteryPercent -ne $CurrentPercent) { | ||
$BatteryPercent=$currentPercent; | ||
WriteBatteryPercent -Append 1; | ||
} | ||
} else { | ||
Write-Output "PowerShell function (Get-WmiObject Win32_Battery) didn't return any information about your battery."; | ||
Write-Output "Maybe it's because you are trying to run this script on desktop PC."; | ||
Write-Output "If not and you are trying to run this script on laptop, then, please, contact your laptop manufacturer."; | ||
pause; | ||
} | ||
Start-Sleep -s 10; | ||
Start-Sleep -Seconds 10; | ||
} | ||
|
||
pause; | ||
pause; |