KIXKicks
(Starting to like KiXtart)
2012-04-23 10:21 PM
Is there an easy way to tell if a Laptop running off battery vs ac power?

Our KIX Script can identify if a computer is a laptop or not, but I need to know if the laptop is connected to power or if it is running from its battery.

Most searches just return if how to identify a laptop vs a desktop.


AllenAdministrator
(KiX Supporter)
2012-04-23 10:23 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

Hmmm... I might know a back door method if we can't find it via wmi... did you try kixomatic?

ShaneEP
(MM club member)
2012-04-23 10:31 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

This might be of use via WMI...Unfortunately I have no way of testing here in the office.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394074%28v=vs.85%29.aspx


Mart
(KiX Supporter)
2012-04-23 10:33 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

Using WMI:
 Code:
$strComputer = @WKSTA

$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\wmi")

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

For Each $objItem in $colItems
	? "Battery: " + $objItem.InstanceName
	? "On AC Power: " + $objItem.PowerOnline
	? "Battery is Discharging: " + $objItem.Discharging
	? "Battery is Charging: " + $objItem.Charging
	? "Remaining capacity: " + $objItem.RemainingCapacity
Next

Sleep 5


AllenAdministrator
(KiX Supporter)
2012-04-23 10:37 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

How Can I Tell Whether a Laptop Computer is Running Off Batteries?
http://blogs.technet.com/b/heyscriptingg...-batteries.aspx

(edit: same code as Marts above)

...but doesn't appear to work with Windows 7. Nevermind... Shane got it working and it does work with Win 7. \:\)



ShaneEP
(MM club member)
2012-04-23 10:39 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

Nice Mart! Just to elaborate a bit more...

 Code:
[postprep]If UsingBattery()
   ? "on battery power"
Else
   ? "plugged in"
Endif

get $

Function UsingBattery()
   $objWMIService = GetObject("winmgmts:\\" + @WKSTA + "\root\wmi")
   $colItems = $objWMIService.ExecQuery("Select * From BatteryStatus Where Voltage > 0")
   For Each $objItem in $colItems
      $UsingBattery = IIF($objItem.PowerOnline,0,1)
   Next
EndFunction


Mart
(KiX Supporter)
2012-04-23 10:54 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

 Originally Posted By: Allen

...
but doesn't appear to work with Windows 7. Nevermind... Shane got it working and it does work with Win 7. \:\)
...


My code also works on Win7. I did not pull the power cord from my laptop because it is in a dock with all kinds of stuff connected but it should work just fine.

Nice UDF Shane. Going to post it in the UDF section? Maybe an optional parameter for the other stuff like remaining capacity and so on could be useful.


AllenAdministrator
(KiX Supporter)
2012-04-23 11:00 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

I don't know why the code didnt work... the vbs didnt work either. The UDF works for me though. The UsingBattery should go in the UDF as is, with another UDF for all the other stuff.

Man KixKicks... you got it good right now with everyone chipping in. \:\)


ShaneEP
(MM club member)
2012-04-23 11:09 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

 Quote:
Man KixKicks... you got it good right now with everyone chipping in
haha...were just excited to see so many question within a week span.

It would make a good UDF Mart...but I pretty much just modified your code and I don't want to steal the credit, so if you want to make one, go for it. If you're too busy then I'll do it.


KIXKicks
(Starting to like KiXtart)
2012-04-24 12:46 AM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

AWESOME, everyone!!!

Thanks for your help...

Maybe there should be an achievement for most questions in a week (newbie award)...LOL


Mart
(KiX Supporter)
2012-04-24 09:48 AM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

 Originally Posted By: ShaneEP

...
It would make a good UDF Mart...but I pretty much just modified your code and I don't want to steal the credit, so if you want to make one, go for it. If you're too busy then I'll do it.


One of our servers went to visit its maker without any chance of reincarnation so I have an SQL installation and database recovery planned for this morning. I'll see if I can find some time later today.


LonkeroAdministrator
(KiX Master Guru)
2012-04-24 01:28 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

oh... historic work. would be kinda nice to do that myself for nostalgy's sake :P

Mart
(KiX Supporter)
2012-04-24 04:36 PM
Re: Is there an easy way to tell if a Laptop running off battery vs ac power?

See: http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=204951#Post204951