Solved

Need VB script to check APC UPS status

Posted on 2008-06-18
18
2,168 Views
Last Modified: 2013-12-20
He there all, i need a script (VB) to check APC UPS status. I have found some PHP scripts but i need it to be in VBscript.  I have found a wmi server called uninteruptablepowersuply but this doesn't return any values.

I already have a large script for checking systemspecs and want to ad this to it, so please no wscript.echo but StrHtmL. (HTML output)
0
Comment
Question by:Dutchict
  • 9
  • 7
  • 2
18 Comments
 
LVL 5

Expert Comment

by:Devario Johnson
Comment Utility
Will a .pl work?  All you have to do is install it on the machine

http://www.deadcat.net/viewfile.php?fileid=65


if you send me the php version I can convert it to ASP for you

Post it here on EE
0
 

Author Comment

by:Dutchict
Comment Utility
Hi, thanks for this script, it does what i want.. but i'd like it to be in my VB script so the output can go in thesame HTML.  Do you know about executing Perl in VB?  
0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
The Win32_UninterruptiblePowerSupply class is dropped as of Windows Vista.  It also only works with serial UPS, not USB, so isn't that useful.  I am just checking for the USB equivalent.  I am 80% sure that there will be a pure VB solution for this.
0
 
LVL 5

Expert Comment

by:Devario Johnson
Comment Utility
JMSSupport

If you post the PHP version you found I may be able to convert it to ASP for you...
0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
The attached script returns the battery status on my laptop.  It should theoretically work on a server with connected batteries too... but my test has failed (though I am not sure if the USB lead of the UPS is plugged into the server, which could explain it).

Could you try this on a server with UPS to see if it works for that?  If so, there are many more properties I can add to the output.
dim colBatteries, objBattery

strComputer="."
 

Set objWMIService = GetObject("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2")

 

Set colBatteries = objWMIService.ExecQuery _

    ("Select * from Win32_Battery")
 

strHTML=strHTML & vbCrLF & vbCrLf & "<br/><br/>Battery Information<br/>" & vbCrLf

	 

for each objBattery in colBatteries

	strHtml=strHTML & "Battery status: " & BatteryStatusInfo(objBattery.BatteryStatus) & vbCrLf & "<br/>"

	strHtml=strHTML & "Description: " & objBattery.Description & vbCrLf & "<br/>"

	strHtml=strHTML & "Estimated runtime (minutes): " & objBattery.EstimatedRunTime & vbCrLf & "<br/>"

	strHtml=strHTML & "Status: " & objBattery.Status  & vbCrLf & "<br/>"

	strHtml=strHTML & "Time To Full Charge (minutes): " & objBattery.TimeToFullCharge & vbCrLf & "<br/>"

	strHtml=strHTML & "Estimated Charge Remaining: " & objBattery.EstimatedChargeRemaining & vbCrLf & "<br/><br/>"

next
 

wscript.echo strHTML

	 
 

function BatteryStatusInfo(intBatteryStatus)

	dim strStatus

	select case intBatteryStatus

		case 1: strStatus="Battery is discharging"

		case 2: strStatus="System is on AC power"

		case 3: strStatus="Battery is fully charged"

		case 4: strStatus="Battery is low"

		case 5: strStatus="Battery is critical"

		case 6: strStatus="Battery is charging"

		case 7: strStatus="Battery is charging and high"

		case 8: strStatus="Battery is charging and low"

		case 9: strStatus="Battery is charging and critical"

		case 11: strStatus="Battery is partially charged"

		case else: strStatus="Unknown"

	end select

	BatteryStatusInfo=strStatus

		

end function

Open in new window

0
 

Author Comment

by:Dutchict
Comment Utility
Thanks, i will test this today!
0
 

Author Comment

by:Dutchict
Comment Utility
Hi there again! Just back from holiday and wedding and stuff :)

This script checks batteries, but not the UPS... i found this script in the CIMV2 base also.
Maybe someone has another solution?
0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
The problem is that the other relevant WMI sections are deprecated.. and won't work on future versions of Windows.  I'll look into it again...
0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
I can rewrite some code to use Win32_UninterruptiblePowerSupply - but this relies on the UPS service.  If the UPS service isn't available, it will fail.  Also, it is not supported in Vista or Windows 2008.  Is this an issue?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
Oh, and apparently that only works with serial attached devices, not USB.
0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
Out of interest... when you tested the script, how was the UPS connected?

According to what I've read, the above script should work for batteries connected by USB - including a UPS.  I will be at a site installing a USB UPS tomorrow, so I can check then, but I was just wondering what you tested on.
0
 

Author Comment

by:Dutchict
Comment Utility
Hi there purplepomegranite,

The ups's are mostly serial  atached (bigger APC) and works with SMTP traps
I have tried to look for scripts but coudn't find any.

The most of our servers are win2003 / win2003 2/ win2000 etc
We will be adding 2008 servers soon, as customers are wanting the new technology

0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
Ok, I'll write a script later that uses the above query, should work with serial attached UPS.
0
 

Author Comment

by:Dutchict
Comment Utility
wow, if you where a girl and pretty you would get a big sloppy kiss! lol
anywayz, thanks in advance!
0
 
LVL 24

Expert Comment

by:purplepomegranite
Comment Utility
The script already posted does work with servers with a USB UPS attached.  Have just tried it against an APC UPS.

So we just need the other script for legacy devices.  Will post it in a while (probably this evening).
0
 
LVL 24

Accepted Solution

by:
purplepomegranite earned 500 total points
Comment Utility
The attached code should work with serial attached devices (so long as the UPS service is running).
dim colUPS, objUPS

strComputer="."

 

Set objWMIService = GetObject("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2")

 

Set colUPS = objWMIService.ExecQuery _

    ("Select * from Win32_UninterruptiblePowerSupply")

 

strHTML=strHTML & vbCrLF & vbCrLf & "<br/><br/>UPS Information<br/>" & vbCrLf

         

for each objUPS in colBatteries

        strHtml=strHTML & "Battery status: " & objUPS.Status & vbCrLf & "<br/>"

        strHtml=strHTML & "Description: " & objUPS.Description & vbCrLf & "<br/>"

        strHtml=strHTML & "Estimated runtime (minutes): " & objUPS.EstimatedRunTime & vbCrLf & "<br/>"

        strHtml=strHTML & "Status: " & objUPS.Status  & vbCrLf & "<br/>"

        strHtml=strHTML & "Estimated Charge Remaining: " & objUPS.EstimatedChargeRemaining & vbCrLf & "<br/><br/>"

next

 

wscript.echo strHTML

Open in new window

0
 

Author Comment

by:Dutchict
Comment Utility
Hi purplepomegranite, thanks again! will test this today, here are the point..
0
 

Author Closing Comment

by:Dutchict
Comment Utility
Thanks again! Hope you also like my scripts till now :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now