Need continuous loop command

I have a script I need to loop continuously with time interval 1 minute.

I am using the script is used for intimate the battery percentage to the user

$results=(Get-CimInstance -ClassName Win32_Battery).EstimatedChargeRemaining if ($results -gt 20) { $ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK $MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Information $MessageBody = "Remove the charger. Your battery percentage $results" $MessageTitle = "Laptop Battery Backup status" $Result = [System.Windows.Forms.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon) } elseif ($results -lt 10) { $ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK $MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Warning $MessageBody = "Connect the charger. Your battery percentage $results" $MessageTitle = "Laptop Battery Backup status" $Result = [System.Windows.Forms.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon) } 
2

1 Answer

This intrigued me so I played around and made some changes you might want.

The code below will not prompt you to disconnect the charger if it is not connected and will not prompt you to connect the charger if it is connected when the appropriate conditions are met.

I've set the full charge and low charge percentages in variables at the top of the script to make them easy to locate and change and I also set them to keep the battery between 20-80% the recommended range for longevity of Lithium-Ion batteries as I understand it.

Also the time between messages set to 3 minutes as batteries shouldn't drain fast enough that they need to be checked every minute, even 3 minutes may be too often.

Lastly, I provided an escape incase you want to turn the program off with the cancel button.

$FullChgPct = 78 $NeedChgPct = 22 $MessageTitle = "Laptop Battery Backup status" $Result = "OK" $ButtonType = [System.Windows.Forms.MessageBoxButtons]::OKCancel While ($Result -eq "OK") { $Battery=Get-CimInstance -ClassName Win32_Battery $ChgPct = $Battery.EstimatedChargeRemaining $Mains = $Battery.Status if ($ChgPct -gt $FullChgPct -and $Mains -eq 2) { $MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Information $MessageBody = "Remove the charger. Your battery percentage $ChgPct" $Result = [System.Windows.Forms.MessageBox]::Show( $MessageBody,$MessageTitle,$ButtonType,$MessageIcon) } If ($ChgPct -lt $NeedChgPct -and $Mains -eq 1) { $MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Warning $MessageBody = "Connect the charger. Your battery percentage $ChgPct" $Result = [System.Windows.Forms.MessageBox]::Show( $MessageBody,$MessageTitle,$ButtonType,$MessageIcon) } # Notify user every 3 Minutes If ($Result -eq "OK") { Start-Sleep -Seconds 300 } } #End While 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like