Skip to content

Commit

Permalink
fixed potential null exception and code beauty adjustments in battery…
Browse files Browse the repository at this point in the history
…_test_log.ps1
  • Loading branch information
Moris-Shalon committed Nov 24, 2020
1 parent 084f155 commit 798972e
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions battery_test_log.ps1
@@ -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;

0 comments on commit 798972e

Please sign in to comment.