Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Lounger
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Create a task based on battery level

    I usually charge my laptop battery to 80% before disconnecting the charger so as to increase the battery life. The problem is that I often forget to check the battery level while charging and find out hours later that it's been at 100%. I would like to create a task in Task Scheduler that displays a message when the battery level reaches 80%. But I don't know how to add "battery level at x%" as a trigger for the task. I would appreciate your help very much.

  2. #2
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,414
    Thanks
    33
    Thanked 195 Times in 175 Posts
    There's no such Task Scheduler 'trigger', so you probably need to run a utility which will pop up a message when your required battery threshold is reached. I found Battery Status Tool, which may or may not do what you want.

    The usual BatteryCare might perhaps assist, alternatively.
    BATcher

    Time prevents everything happening all at once...

  3. #3
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,188
    Thanks
    47
    Thanked 983 Times in 913 Posts
    If the battery is Lithium Ion there is no need to bother. These batteries have no discernible memory effect and full charge doesn't reduce life.

    If you still want to play I suggest you use WMI - built in to Windows - to check the current level. Download Scriptomatic to play with WMI.

    cheers, Paul

  4. #4
    New Lounger
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried both of those tools but neither offer the required function.

    I downloaded Scriptomatic 2.0 but it doesn't run properly for some reason. It's an html application and while it launches properly, it gives a "script error on this page, do you want to continue" error akin to the old days when I used to use Internet Explorer on an incompatible site (screenshot). I am running Windows 7 Home Premium and have Internet Explorer 9 (though I use Firefox as my browser). Because of this problem, most of the buttons are not usable (the drop down lists for example).

    Is it because the Scriptomatic is outdated or that my Windows version (home premium) lacks some component?

  5. #5
    5 Star Lounger
    Join Date
    Dec 2009
    Location
    Slough, Berkshire UK
    Posts
    924
    Thanks
    55
    Thanked 52 Times in 50 Posts

    Cool

    Quote Originally Posted by asifbaig View Post
    I tried both of those tools but neither offer the required function.

    I downloaded Scriptomatic 2.0 but it doesn't run properly for some reason. It's an html application and while it launches properly, it gives a "script error on this page, do you want to continue" error akin to the old days when I used to use Internet Explorer on an incompatible site (screenshot). I am running Windows 7 Home Premium and have Internet Explorer 9 (though I use Firefox as my browser). Because of this problem, most of the buttons are not usable (the drop down lists for example).

    Is it because the Scriptomatic is outdated or that my Windows version (home premium) lacks some component?
    I suspect that you need win 7 Pro minimum to get it to work as Home premium does not allow access to some of the hidden system settings. (GU-edit i think it may be called, maybe some one can correct this if incorrect as very likely).
    Clive

    All typing errors are my own work and subject to patents pending. Except errors by the spell checker. And that has its own patients.

  6. #6
    Super Moderator jwitalka's Avatar
    Join Date
    Dec 2009
    Location
    Minnesota
    Posts
    6,794
    Thanks
    117
    Thanked 799 Times in 720 Posts
    I think you're referring to the General Policy Editor (gpedit) that is not available in the Home Premium version Clive.

    Jerry

  7. #7
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,188
    Thanks
    47
    Thanked 983 Times in 913 Posts
    Scriptomatic requires you to have admin credentials to run it. If you have UAC enabled you need to run it from an administrator Command Prompt.
    Right click on "Command Prompt" and select "Run as administrator".
    Enter the full path to Scriptomatic, e.g. D:\Temp\Scriptomatic\scriptomaticv2.hta

    cheers, Paul

  8. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    PowerShell Low/High Battery Charge Checker

    asifbaig,

    Here's a possible solution. It may not be pretty but it does work.
    Place the following PowerShell program in your default PowerShell Directory.
    Code:
    Param (
     [Parameter (Mandatory=$False)]
       $BaseCapacity
       )
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    $MyBattery = Get-WmiObject -class BatteryStatus -ComputerName LocalHost -NameSpace  root\WMI
    $BatteryRemaining = $MyBattery.RemainingCapacity/$BaseCapacity
    
    if(($BatteryRemaining -lt 0.87) -and $MyBattery.Discharging) {
       $Message = "Battery Low...Please Charge Me!"
      [Windows.Forms.MessageBox]::Show($Message, "Battery Status", `
        [Windows.Forms.MessageBoxButtons]::OK , `
        [Windows.Forms.MessageBoxIcon]::Information , `
        [Windows.Forms.MessageBoxDefaultButton]::Button1, `
        [Windows.Forms.MessageBoxOptions]::ServiceNotification)
    }
    Note code also included in attachment.

    Next create a scheduled task that will fire every hour (or time interval of your choice ).
    TSAction.PNG
    Note: You need to pass to the program {after the program name in the Action pane of the scheduled task } the mWh ( milla Watt Hours ) FULL capacity of your battery! I used the Battery Meter program to find this value. So the optional parameters would look like d:\path\BatteryStatus.ps1 7730

    Done!

    When the task fires you will get the following message if the battery is below the threshold and you are NOT plugged in.
    LowBatteryMsg.PNG
    See the messy part it shows a command window along with the message and that window will flash even if the message doesn't display. When you click OK both windows close.

    The code as written checks for low battery since that was the easiest to check as I was writing the code. You can easily change the test from -le to -ge and also the percentage that follows to your desired threshold levels. You could also make the code check for both low & high charge conditions and print the appropriate message. BTW don't forget to change the $Message value as appropriate.

    HTH
    Last edited by RetiredGeek; 2013-03-06 at 15:24.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  9. #9
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Hey Y'all,

    Here's the latest version that kicks out messages for both Unplugged/Low Battery and Plugged in Charged Battery:
    Code:
    <#+---------------------------------------------------------------------------+
      | PowerShell Pgm: BatteryStatus.ps1                                         |
      | Programmed By : The Computer Mentor aka Retired Geek aka                  |
      | Created       : 06 Mar 2013                                               |
      | Last Updated  :                                                           |
      | Current Ver.  : 3.0                                                       |
      +---------------------------------------------------------------------------+
    #>
    Param (
     [Parameter (Mandatory=$False)]
       $BaseCapacity
       )
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    $MyBattery = `
     Get-WmiObject -class BatteryStatus -ComputerName LocalHost -NameSpace  root\WMI
    $BatteryRemaining = $MyBattery.RemainingCapacity/$BaseCapacity
    $Message = ""
    
    if(($BatteryRemaining -lt 0.30) -and $MyBattery.Discharging) {
       $Message = "Battery Low...Please Charge Me!"
    }
    Elseif(($BatteryRemaining -gt 0.95) -and $MyBattery.PowerOnline) {
       $Message = "Battery CHARGED above 95%...Please Unplug Me!"
    }
    
    if($Message -ne "") {
      [Windows.Forms.MessageBox]::Show($Message, "Battery Status", `
        [Windows.Forms.MessageBoxButtons]::OK , `
        [Windows.Forms.MessageBoxIcon]::Information , `
        [Windows.Forms.MessageBoxDefaultButton]::Button1, `
        [Windows.Forms.MessageBoxOptions]::ServiceNotification)
    }
    <#
      +___________________________________________________________________________+
      | Notes:                                                                    |
      | 1. To call as a scheduled task do the following in the Action Pane        |
      |   A. Action:                   Start a Program                            |
      |   B. Program/script:           powershell.exe                             |
      |   C. Add arguments (optional): d:\path\BatteryStatus.ps1   xxxx           |
      |       Where: xxxx is the number of mWh of a full charge on your battery   |
      +---------------------------------------------------------------------------+
    #>
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  10. #10
    New Lounger
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much, good sir. I will try this out and let you know how it goes. My exams are close so it may be a while before I get around to doing this but I will definitely report the results.

  11. #11
    New Lounger
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aaaaaand I'm back. In less than half a year. As promised. ;-)

    I tried the above mentioned script and I got this error:


    Cannot find the type for custom attribute 'Parameter '. Make sure that the asse
    mbly that contains this type is loaded.
    At D:\Stuff\Unsorted\Powershell\chargealert.ps1:10 char:13
    + [Parameter <<<< (Mandatory=$False)]
    + CategoryInfo : InvalidOperation: (Parameter :Token) [], Runtime
    Exception
    + FullyQualifiedErrorId : CustomAttributeTypeNotFound


    It appears that I need to install some module or enable some setting. Kindly let me know what I'm doing wrong.

  12. #12
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Could you please post your version of the .ps1 file?
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  13. #13
    New Lounger
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I copied all the text that you gave, saved it to a text file and named it chargealert.ps1.

    I'll paste the text below. In case there's some formatting error on this website, you can get the actual file from here: https://dl.dropboxusercontent.com/u/...hargealert.ps1

    Code:
    <#+---------------------------------------------------------------------------+
      | PowerShell Pgm: BatteryStatus.ps1                                         |
      | Programmed By : The Computer Mentor aka Retired Geek aka                  |
      | Created       : 06 Mar 2013                                               |
      | Last Updated  :                                                           |
      | Current Ver.  : 3.0                                                       |
      +---------------------------------------------------------------------------+
    #>
    Param (
     [Parameter (Mandatory=$False)]
       $BaseCapacity
       )
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    $MyBattery = `
     Get-WmiObject -class BatteryStatus -ComputerName LocalHost -NameSpace  root\WMI
    $BatteryRemaining = $MyBattery.RemainingCapacity/$BaseCapacity
    $Message = ""
    
    if(($BatteryRemaining -lt 0.40) -and $MyBattery.Discharging) {
       $Message = "Battery Low...Please Charge Me!"
    }
    Elseif(($BatteryRemaining -gt 0.85) -and $MyBattery.PowerOnline) {
       $Message = "Battery CHARGED above 95%...Please Unplug Me!"
    }
    
    if($Message -ne "") {
      [Windows.Forms.MessageBox]::Show($Message, "Battery Status", `
        [Windows.Forms.MessageBoxButtons]::OK , `
        [Windows.Forms.MessageBoxIcon]::Information , `
        [Windows.Forms.MessageBoxDefaultButton]::Button1, `
        [Windows.Forms.MessageBoxOptions]::ServiceNotification)
    }
    <#
      +___________________________________________________________________________+
      | Notes:                                                                    |
      | 1. To call as a scheduled task do the following in the Action Pane        |
      |   A. Action:                   Start a Program                            |
      |   B. Program/script:           powershell.exe                             |
      |   C. Add arguments (optional): d:\path\BatteryStatus.ps1   xxxx           |
      |       Where: xxxx is the number of mWh of a full charge on your battery   |
      +---------------------------------------------------------------------------+
    #>

  14. #14
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Asifbaig,

    Ok, here goes with a "stupid" question. You are running this on a machine that has a battery?
    I tried it on my desktop and got errors although not the same as yours:

    Get-WmiObject : Invalid class "BatteryStatus"
    At G:\bekdocs\scripts\BatteryStatusV3.ps1:19 char:2
    + Get-WmiObject -class BatteryStatus -ComputerName LocalHost -NameSpace root\WMI
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidType: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Com mands.GetWmiObjectCommand

    But it ran just fine on my laptop.
    BatteryStatus.PNG
    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  15. #15
    New Lounger
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, I ran it on my laptop with the battery still attached to it (either that or my desktop became self-aware, disguised itself and is planning to kill me ;-) ).

    And surprisingly, I ran it now and it runs fine. I restarted the system in between and that must have evacuated the gremlins... :-) Thank you so much for your help.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •