From 084f155ba786b2664c4e10650b958341acc1b1a5 Mon Sep 17 00:00:00 2001 From: ZChuckMoris <18584322+ZChuckMoris@users.noreply.github.com> Date: Mon, 23 Nov 2020 16:04:59 +0300 Subject: [PATCH] battery test fixed high cpu usage --- battery_test_log.ps1 | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/battery_test_log.ps1 b/battery_test_log.ps1 index 73676a7..fd4cb66 100755 --- a/battery_test_log.ps1 +++ b/battery_test_log.ps1 @@ -1,4 +1,4 @@ -function WriteBatteryPercent ([int]$Append, [int]$Sleep) { +function WriteBatteryPercent ([int]$Append) { if ($Append -ne 0) { Write-Output (([string](Get-WmiObject Win32_Battery).EstimatedChargeRemaining) + ';' + (Get-Date -Format "%H:%m:s")) ` | Tee-Object -Filepath "battery_test_log.txt" -Append; @@ -6,21 +6,20 @@ function WriteBatteryPercent ([int]$Append, [int]$Sleep) { Write-Output (([string](Get-WmiObject Win32_Battery).EstimatedChargeRemaining) + ';' + (Get-Date -Format "%H:%m:s")) ` | Tee-Object -Filepath "battery_test_log.txt"; } - if ($Sleep -ne 0) { - Start-Sleep -s $Sleep - } } -Write-Output "battery percentage;time" | Tee-Object -Filepath "battery_test_log.txt"; -$BatteryPercent = (Get-WmiObject Win32_Battery).EstimatedChargeRemaining; -WriteBatteryPercent -Append 0 -Sleep 10; +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; +} while ($true) { ` if ($BatteryPercent -ne (Get-WmiObject Win32_Battery).EstimatedChargeRemaining) { $BatteryPercent = (Get-WmiObject Win32_Battery).EstimatedChargeRemaining; - WriteBatteryPercent -Append 1 -Sleep 10; + WriteBatteryPercent -Append 1; } + Start-Sleep -s 10; } pause; \ No newline at end of file