fortypoundhead.com

Using VBScript and WMI to get Remote System Info

Posted On 2016-02-09 by VB6Boy
Keywords:
Tags: VBScript Tip Tutorial Windows
Views: 1364


From time to time, you might be called upon to get some information about many machines, quickly. If you don't have an up-to-date inventory system, or that data is suspect, you can leverage a script to get the data for you.

I built the below quick and dirty script some time ago to gather processor and memory configurations for about 150 hosts scattered across several sites. Running the script, I was able to successfully grab the Number of CPUs, the speed of the CPU(s), and the amount of memory in each machine, in just a few minutes.

The script below expects to find a file called input.txt in the current directory. The input file should contain a list of host names, one per line. Each host will queried in turn, and the results of the queries will appended to a file called output.txt, also in the current directory. Original filenames, eh? ;)

Hope this helps someone out there, and if you have any comments, questions, complaints, or improvements, feel free to start a thread in the forums, or simply post a comment below.

'*** Set aside some variables

Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
Dim strComputer

'*** Define some values

strDirectory = "."
strFile = "\output.txt"
strInputFile="input.txt"

'*** instantiate file system object for reading input file

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInputFile, ForReading)

Const ForReading = 1

Dim arrFileLines()
i = 0

'*** read the input file into the array

Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

'*** instantiate file system object for writing the output

Set objFSO = CreateObject("Scripting.FileSystemObject")

'*** check to see if the file exists. if not, create it

If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If

set objFile = nothing
set objFolder = nothing

'*** using append here. if the file was just created in
'*** the previous step, we'll still use append. this way,
'*** we can also append to an existing file.

Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, 2, True)
On Error Resume Next ' bad form, i know.

'*** write a little header first

objTextFile.WriteLine("Hostname,CPUs,Memory,Speed")

'*** roll through the array, and use remote wmi queries to get the info
'*** from the remote system, one at a time. Note that the account running
'*** the script must have permission to query wmi on the remote system.

For Each strLine in arrFileLines

strComputer=strLine ' current host name


Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\cimv2")

Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")

Set objWMICPU = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colCPU = objWMICPU.ExecQuery("Select * from Win32_Processor",,48)

For Each objComputer in colSettings

strText=strComputer & "," & objComputer.NumberOfProcessors & "," &_
objComputer.TotalPhysicalMemory & ","

Next

For each objComputer in colCPU

strMaxClockSpeed=objComputer.MaxClockSpeed

Next

'*** Write the output to the file, move on to the next

strText=strText & strMaxClockSpeed
objTextFile.WriteLIne(strText)

Next

'*** close our files up

on error goto 0
objTextFile.Close


About the Author

VB6Boy has posted a total of 7 articles.

 


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.162.19.123

Before you can post, you need to prove you are human. If you log in, this test goes away.


Beat With A Stick List Returns: By request, BWASL has returned to the site. Free your rage by telling the world about something that really grinds your gears. Add your beating.



Recent Forum Posts

BWASL returns
dwirch posted on May 13, 2017 at about 15:24 in Site News

BWASL returns
dwirch posted on May 13, 2017 at about 8:46 in Site News

Job Spammer: Balashankar Bose Bose
dwirch posted on May 11, 2017 at about 10:05 in Spammers

Job Spammer: Bharti Jigyasi
dwirch posted on May 11, 2017 at about 7:58 in Spammers

List of Shady Characters
dwirch posted on April 25, 2017 at about 16:39 in Webmaster Stuff

Job Spammer: Bilal Uddin
dwirch posted on April 25, 2017 at about 11:00 in Spammers