Saturday 3 June 2017

Script to Monitor Battery Percentage

Hi friends,

Recently, I got a new battery for my Dell laptop and was wondering how to use the battery in the most efficient way. I then got to read a Quora anwser by Shubham Deshmukh as to how to use the battery in the most efficient way where he suggested to keep the battery percentage between 40 and 60 to get a longer battery life. This made me think about writing a script that monitors my battery and pops a message box alerting me when the battery percentage goes beyond the above range. The present script monitors the battery status every half hour which can be changed as per your requirement. 

set locatorObj = CreateObject("WbemScripting.SWbemLocator")
set servicesObj = locatorObj.ConnectServer(".", "root\wmi")
set results = ServicesObj.ExecQuery("select * from batteryfullchargedcapacity")

for each resultObj in results
 capacityInfo = resultObj.FullChargedCapacity
next

Do While 1
 set results = servicesObj.ExecQuery("select * from batterystatus")
 for each resultObj in results
  remainingPercent = resultObj.RemainingCapacity
  charging = resultObj.Charging
 next
 percent = ((remainingPercent / capacityInfo) * 100) mod 100
 if charging and (percent > 60) Then 
  msgbox "Battery " & percent & "% charged", vbInformation, "Battery monitor"
 end if
 if not charging and (percent < 40) Then 
  msgbox "Battery " & percent & "% remaining", vbInformation, "Battery monitor"
 end if
 wscript.sleep 180000 ' 5 minutes
Loop

The two if conditions determines whether the battery is above 60% and below 40% respectively and pops a message box with the appropriate information. 

Information to run the script:
You are required to paste the above code into a text editor and save it as any_name.vbs (set the save as dialogue to save as 'all files'). Save this file to any location of your choice and create a shortcut for it by clicking right and selecting the 'Create Shortcut' option. Next, you need to paste the created shortcut in your start-up folder to avoid the need to run it manually every time
Share:

5 comments:

  1. Hey man, it's Aakash from the email conversation that we had the other day. I just wanted to know how you got to know where and what library you'd have to use to write an administrative script and how to develop this kind of script fast? I mean I don't think you'd have sat through the entire MSDN documentation for this, did you?

    ReplyDelete
  2. Hi Aakash,

    I didn't read the complete documentation obviously. I little of Google search and the following links (https://gist.github.com/0xABD/1591886, https://gallery.technet.microsoft.com/scriptcenter/b96bd8b5-64af-4c3d-aea9-e2de22611109, http://www.tek-tips.com/viewthread.cfm?qid=1614797) and a few more helped me get what I wanted.

    ReplyDelete
  3. Hi Krishna, In your code, The sleep for the script is not correct:
    wscript.sleep 180000 ' 5 minutes => 180000 milli seconds is 3 mins and not 5 mins. Just add one more zero to correct it.

    ReplyDelete
  4. Currently your script is getting executed every 3 mins and not every half an hour as you have mentioned. Just FYI. Thanks.

    ReplyDelete
  5. Thanks @Krishna Chaurasia. I have used this script in automating my own Laptop Battery charging process.

    ReplyDelete

Contact Me

Name

Email *

Message *

Popular Posts