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