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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powercfg report

Status
Not open for further replies.

PaladinS

IS-IT--Management
Jan 30, 2015
7
US
Hello all,

Is it possible to use PowerShell or a VBscript to get a Battery information report on multiple laptops, the idea is to run a script on hundreds of machines, get the ComputerName , CurrentCapacity , DesignCapacity , ComputerModel

Then have that information written to a Database , using Powercfg tried writing the output to a CSV file but it didn't work well because the machines were all trying to write to the same file at the same millisecond ; i am thinking a database file .mdb would allow multiple entries simultaneously ! but i am not a 100% sure

Any ideas ?
 
The way we do this kind of thing at my job is we have the individual machines write to a files that has their name in it, so there are multiple individual files, then we run a script that combines the data for us so we don't have to worry about the machines all trying to write to the same file.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Thanks Mark,

We are exactly there! I got to the point were I have the Battery Data in multiple files , each file is named after the Laptop it came from, but when we try to combine or merge the data to.. lets say a CSV file , that is when we run into problems, how did you combine your files into one file or one database without running into the simultaneous access issue? thanks for your input
 
How are you trying to do it?

Lets say your source files are all CSV files. Those are just text files. Setup your script to enumerate all the files in the folder they reside. Then loop through the array of files and add read their contents and append that to your new file. I assume you know how to read and write tot he files so here is the logic you are looking for accessing the source data without it being locked. Something like this:
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = objFSO.GetFolder("C:\SourceFiles")

For Each oFile in oFolder.Files
[indent]'Code to do the work to read and write contents goes in here[/indent]
Next

Note that I would recommend you have the source files generated by a scheduled task at one time and the combining action take place at a later time to ensure no data is being written.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Thanks again , i'll try that and see how it goes
 
I have the script to get the Battery wear level working , but i am running into a concurrency issue when it writes to multiple machines all at once , like 20 or 50 laptops , is there a way around this


'Collects battery data of one or more Laptops.
dim strComputerName, WindowsVer, i, DesignCapacity, CurrentCapacity, ComputerModel

'determine windows version
Set SystemSet = GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
for each System in SystemSet
WindowsVer = System.Version

next

strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Computersystem")
For Each Item In colItems
ComputerModel = Item.Model

Next

Set objMemory = Nothing
Set objWMIService = Nothing

'find the test folder
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FolderExists("c:\TEST64") then
localPath="c:\TEST64"

else

localPath="c:\TEST"

end if

'get computer's name

SET wshShell = WScript.CreateObject( "WScript.Shell" )

strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

'determine State name and Office name

State = left(strComputerName,2)
Office = left(strComputerName,5)

'create report folders if they don't exist

if not fso.FolderExists("\\ServerShare\BatteryData\" & State) then
fso.CreateFolder "\\ServerShare\BatteryData\" & State
end if

if not fso.FolderExists("\\ServerShare\BatteryData\" & State & "\" & Office) then
fso.CreateFolder "\\ServerShare\BatteryData\" & State & "\" & Office

end if


'run the battery report

set WSHShell = WScript.CreateObject("WScript.Shell")
set cmdOutput = WSHShell.Exec("c:\windows\system32\powercfg.exe -energy -xml -duration 0 -output " & localpath & "\BatteryData.xml")
Set xDoc = CreateObject("Msxml2.DOMDocument")
xDoc.Load(localpath & "\BatteryData.xml")
Set Nodes = xDoc.childNodes
Set NameList = xDoc.getElementsByTagName("Name")
for each name in NameList
if name.text = "Design Capacity" then
set pNode = name.parentNode
DesignCapacity = pNode.childNodes(1).text
end if
if name.text = "Last Full Charge" then
set pNode = name.parentNode
CurrentCapacity = pNode.childNodes(1).text
end if
next


dim provider : provider = "microsoft.jet.oledb.4.0"
dim db : db = "\\ServerShare\BatteryData\" & State & "\" & Office & "\" & Office & "_Consolidated.mdb"
dim ds : ds = "provider=" & provider & "; data source=" & db


'create mdb file if it doesn't exist
if not fso.FileExists(db) then

dim catalog : set catalog = createobject("adox.catalog")
catalog.create ds
set conn = createobject("adodb.connection")
conn.open ds

sql = "create table BatteryData (id int NOT NULL IDENTITY, LaptopName varchar(16),CurrentCapacity int,DesignCapacity int,LaptopModel varchar(40),WearLevel int)"
conn.Execute sql
end if

set conn = createobject("adodb.connection")
conn.open ds

dim WearLevel
WearLevel = cInt(CurrentCapacity / DesignCapacity * 100)


sql = "insert into BatteryData (LaptopName, CurrentCapacity, DesignCapacity, LaptopModel, WearLevel) values ('"& strComputerName &"', '" & CurrentCapacity & "',' " & DesignCapacity & "', '" & ComputerModel & "', '" & WearLevel & "')"
conn.Execute sql


conn.close
set conn = nothing



 
Convert your MDB file to a SQL database. You can use SQL Express if you need a free SQL server. It will be more robust for the simultaneous adds.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
What is the "concurrency issue"? Is there an error message?
 
Thanks Mark,SQL was a nice thought that presented itself earlier and i was going to dump all the data there , but the management teams would like it to be in an Access database format per office, they don't want the idea of running a quarry or using a quarry GUI to look at the data. guitarzan; as for your question about the concurrency issue , when we run the job on 20 laptops remotely , they all try to write to the same MDB file together or within milliseconds from each other, that causes the issue of missing data , so instead of the mdb table showing 20 records , it only shows like 4 sometimes 11 , sometimes 8
 
I'd still go with SQL, you can link to a SQL table in access so it would behave the exact same way for reports and data access.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
I could be wrong, but I don't think 20 writes at or about the same time would cause the inserts to fail, and even if it did you should see an actual error message, not just missing records. Are you sure the script is executing when you expect it to?

I'm not disagreeing about using SQL server, but if that's not an option you might want to put some logging into the script
 
I am running the script from a symantec deployment Console remotely on 20 laptops at a time , they all report back that the script ran successfully , but that doesn't mean that there is no error , i'll change my parameters to unhide the window and run it here on five machines and watch it as it runs.
 
I ran the script on 20 machines here at the office and watched the screens , it ran successfully on all 20 machines and i watched the MDB file on the server create the Temp-Lock file as it should when the records are being inserted , but only 16 records showed!! that is better than 8 or 11 , but still , i should be getting 20 records , i watched the machines that didn't report and they didn't display any errors at all! i am not familiar with adding logs to a scripts to capture errors, and i am not sure if using adLockOptimistis would help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top