Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Battery Information with VBscript?

Status
Not open for further replies.

XaRen

Programmer
Aug 4, 2010
3
DE
Hello everyone,

I am totally new to visual basic script and I am not sure if I will need it for more than my current problem. I need a simple VBScript to check the remaining power in a notebook battery and save the percentage into a text-file. This script is executed from another programm on load.

I am already able to create a text file. I searched the web the last two days and found a lot of visual basic code but when I tried to run this in my VBScript I got a lot of errors. I narrowed down retrieving the battery information to the following code lines:

Dim psBattery As SystemInformation = SystemInformation.PowerStatus
Dim perFull As String = psBattery.BatteryLifePercent

BUT it is not working. I think it is the declaration with "As" that's causing the problem. Or am I doing this in a totally wrong way?

Can anyone help me with that? That would be great!

Thanks in advance,
Christine
 
Thanks for your really detailed answer ;)

I don't know what to do with it. Could you explain it to me, please?

Thanks,
Christine
 
well, its a WMI class allows you to access certain properties and methods of the battery in a computer? a little google on Win32_Battery class yields the below, this might help you?

'On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Battery",,48)
For Each objItem in colItems
Wscript.Echo "Availability: " & objItem.Availability
Wscript.Echo "BatteryRechargeTime: " & objItem.BatteryRechargeTime
Wscript.Echo "BatteryStatus: " & objItem.BatteryStatus
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Chemistry: " & objItem.Chemistry
Wscript.Echo "ConfigManagerErrorCode: " & objItem.ConfigManagerErrorCode
Wscript.Echo "ConfigManagerUserConfig: " & objItem.ConfigManagerUserConfig
Wscript.Echo "CreationClassName: " & objItem.CreationClassName
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "DesignCapacity: " & objItem.DesignCapacity
Wscript.Echo "DesignVoltage: " & objItem.DesignVoltage
Wscript.Echo "DeviceID: " & objItem.DeviceID
Wscript.Echo "ErrorCleared: " & objItem.ErrorCleared
Wscript.Echo "ErrorDescription: " & objItem.ErrorDescription
Wscript.Echo "EstimatedChargeRemaining: " & objItem.EstimatedChargeRemaining
Wscript.Echo "EstimatedRunTime: " & objItem.EstimatedRunTime
Wscript.Echo "ExpectedBatteryLife: " & objItem.ExpectedBatteryLife
Wscript.Echo "ExpectedLife: " & objItem.ExpectedLife
Wscript.Echo "FullChargeCapacity: " & objItem.FullChargeCapacity
Wscript.Echo "InstallDate: " & objItem.InstallDate
Wscript.Echo "LastErrorCode: " & objItem.LastErrorCode
Wscript.Echo "MaxRechargeTime: " & objItem.MaxRechargeTime
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "PNPDeviceID: " & objItem.PNPDeviceID
Wscript.Echo "PowerManagementCapabilities: " & objItem.PowerManagementCapabilities
Wscript.Echo "PowerManagementSupported: " & objItem.PowerManagementSupported
Wscript.Echo "SmartBatteryVersion: " & objItem.SmartBatteryVersion
Wscript.Echo "Status: " & objItem.Status
Wscript.Echo "StatusInfo: " & objItem.StatusInfo
Wscript.Echo "SystemCreationClassName: " & objItem.SystemCreationClassName
Wscript.Echo "SystemName: " & objItem.SystemName
Wscript.Echo "TimeOnBattery: " & objItem.TimeOnBattery
Wscript.Echo "TimeToFullCharge: " & objItem.TimeToFullCharge
Next
 
I was able to access the battery information using the win32_battery class. Thanks again for that. I was also able to write the received information into a text file. Unfortunately the windows explorer pops up, when there is no text file during creation. Is it possible to prevent that? I don't want my user to know, that I create this file. I need it, to read it with a flash asset, so there's no use for the user to know about this file.

Can anyone help me with that? Thanks a lot in advance,
Christine
 
Here's code similar to what I use to write to text files, no explorer pops up.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

	Set NewFile = objFSO.CreateTextFile("C:\temp\textfile.txt", True)
	NewFile.WriteLine("Line 1 added to a text file")
	NewFile.WriteLine("Line 2 added to a text file")
	NewFile.Close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top