Script Tips
Roll Call
A handy script that can list each computer and list its latest service pack.
Here's a script you'll want to keep handy: It reads a list of names from a text file, and then allows you to do something with each. It’s really a template, but I’ve made a complete script by having it treat each name as a computer name and using WMI to check each computer’s latest service pack. You’ll notice that I’ve added some error-checking to cover computers which aren’t reachable by the script.
Dim strFilename
strFilename = "C:\computers.txt"
Sub DoObject(strName)
'this is where YOUR code goes: strName will
'contain current computer (or whatever) name.
'In this example, we'll use it to query
'the service pack number.
'Notice how the error trapping works.
'first, turn on error trapping
On Error Resume Next
'try to make a WMI connection - note the use of the
'computer name from our strName variable
Dim objWMI, colOS, objOS
Set objWMI = GetObject("winmgmts:\\" & _
strName & "\root\cimv2")
'did an error occur?
If Err <> 0 Then
WScript.Echo "Couldn't connect to " & _
strName
Else
'execute WMI query
Set colOS = objWMI.ExecQuery("SELECT " & _
"ServicePackMajorVersion FROM " & _
"Win32_OperatingSystem")
'go through each returned object
For Each objOS In colOS
WScript.Echo strName & _
" is on SP " & _
objOS.ServicePackMajorVersion
Next
End If
'turn off error trapping
On Error GoTo 0
End Sub
Dim objFSO, objTS, strName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(strFilename)
Do Until objTS.AtEndOfStream
strName = objTS.ReadLine
WScript.Echo "Read " & strName & " from file..."
DoObject strName
Loop
objTS.Close
You can paste your own code into the DoObject subroutine to do whatever you want. You can download a version of the script from the Essentials page on ScriptingAnswers.com; it's definitely one you’ll get plenty of use out of.
About the Author
Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is Curriculum Director for IT Pro Content for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.