Search Tools Links Login

Using VBScript and WMI to get Remote System Info


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 this post

Posted: 2016-02-09
By: vb6boy
Viewed: 6,213 times

Categories

Tip

Tutorials

Visual Basic Script (VBS)

Windows

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.