WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: gibberish on October 15, 2018, 01:48:05 PM

Title: Is computer running on battery power
Post by: gibberish on October 15, 2018, 01:48:05 PM
Hey guys,

I need to know if computer is running on battery power. I don't believe WB can do this natively, but I found this script that suggests how it can be done. I believe current versions of WB can run this code (I am up to date) -- but I don't know how to wrap it (or whatever must be done).

And I'm not "committed" to this code - it's just the only thing I've found that might work. If there is a simpler/better way, I'm all ears.

So how should the script be modified to work with WB? My desired output is some kind of boolean -- either a var gets set to 1/0 or a file is written to the folder or some such.

I am reasonably good with WinBatch but know zip-at-all about wshell / powershell / etc.

Can someone lend a hand?

UPDATE:

Just found probably-better code - but again, unsure how to implement into a WB script:

https://stackoverflow.com/a/241163/1447509 (https://stackoverflow.com/a/241163/1447509)

Boolean isRunningOnBattery =
      (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus ==
       PowerLineStatus.Offline);


=========================================
OLD CODE EXAMPLE
=========================================

From https://blogs.technet.microsoft.com/heyscriptingguy/2007/04/09/how-can-i-tell-whether-a-laptop-computer-is-running-off-batteries/ (https://blogs.technet.microsoft.com/heyscriptingguy/2007/04/09/how-can-i-tell-whether-a-laptop-computer-is-running-off-batteries/)

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\wmi")


Set colItems = objWMIService.ExecQuery("Select * From BatteryStatus Where Voltage > 0")


For Each objItem in colItems

    Wscript.Echo "Battery: " & objItem.InstanceName

    Wscript.Echo "On AC Power: " & objItem.PowerOnline

    Wscript.Echo "Battery is Discharging: " & objItem.Discharging

    Wscript.Echo "Battery is Charging: " & objItem.Charging

    Wscript.Echo "Remaining capacity: " & objItem.RemainingCapacity

Next
Title: Re: Is computer running on battery power
Post by: td on October 16, 2018, 06:58:49 AM
Being that you are reasonably good at WinBatch script, it is a bit surprising that you are not familiar WMI but no matter.  Converting VBS scripts like your second example is relatively straightforward.  You can find numerous examples (maybe hundreds?) in the Tech Database to learn from.  Just search on "WMI" and scroll down the results.

The first example has nothing to do with Powershell and is not ostensibly any better than WMI.  It is c# .Net Foundation Class Library code.   It is also relatively easy to translate to WIL:

Code (winbatch) Select
;; Load the assembly.
ObjectClrOption('useany', 'System.Windows.Forms')

;; Create SystemInformation and PowerLineStatus class instances.
objSysInfo = ObjectClrNew('System.Windows.Forms.SystemInformation')
objPowStat = ObjectClrNew('System.Windows.Forms.PowerLineStatus')


if objSysInfo.PowerStatus.PowerLineStatus ==  objPowStat.Offline
   strText = 'On battery'
else
   strText = 'On line power' ;; May be something else but good enough for example purposes.
endif

Message('Current Power Status', strText)
 
Title: Re: Is computer running on battery power
Post by: gibberish on October 16, 2018, 07:32:23 AM
Yeah, believe it or not, I actually am quite proficient with WB - but I've never before done anything, at all, with WMI or WFC. I have zero knowledge of C, C#, C++, Powershell, etc. I blame that squarely on WinBatch - up until now, after hundreds of scripts (the longest being over 10K lines sans comments), I've never needed to know anything else.

Many thanks Tony. You've identified what I need to study, showed me where to start in the tech DB, and I'm off and running into a new learning curve.