Page 1 of 1 1
Topic Options
#204929 - 2012-04-23 10:21 PM Is there an easy way to tell if a Laptop running off battery vs ac power?
KIXKicks Offline
Starting to like KiXtart

Registered: 2002-07-26
Posts: 177
Loc: Vancouver, WA
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.


Edited by KIXKicks (2012-04-23 10:21 PM)

Top
#204931 - 2012-04-23 10:23 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: KIXKicks]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
Hmmm... I might know a back door method if we can't find it via wmi... did you try kixomatic?
Top
#204933 - 2012-04-23 10:31 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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

Top
#204934 - 2012-04-23 10:33 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Allen]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
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
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204936 - 2012-04-23 10:37 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Allen]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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. \:\)



Edited by Allen (2012-04-23 10:41 PM)

Top
#204937 - 2012-04-23 10:39 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Mart]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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


Edited by ShaneEP (2012-04-23 10:39 PM)

Top
#204941 - 2012-04-23 10:54 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Allen]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
 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.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204942 - 2012-04-23 11:00 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Mart]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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. \:\)

Top
#204943 - 2012-04-23 11:09 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
 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.

Top
#204944 - 2012-04-24 12:46 AM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: ShaneEP]
KIXKicks Offline
Starting to like KiXtart

Registered: 2002-07-26
Posts: 177
Loc: Vancouver, WA
AWESOME, everyone!!!

Thanks for your help...

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

Top
#204945 - 2012-04-24 09:48 AM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: ShaneEP]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
 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.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204948 - 2012-04-24 01:28 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Mart]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
oh... historic work. would be kinda nice to do that myself for nostalgy's sake :P
_________________________
!

download KiXnet

Top
#204952 - 2012-04-24 04:36 PM Re: Is there an easy way to tell if a Laptop running off battery vs ac power? [Re: Lonkero]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
See: http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=204951#Post204951
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.067 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org