Find XP Service Pack Version

By | February 4, 2006

Recently I needed to take a survey of one of my clients sites to determine which service pack versions were running on the machines. As opposed to going to each pc or running some bulky software, I came across a script which will read hostnames off a text file and output the XP Service Pack versions of the machines on the network. This is a quick way to find your service pack versions of your network XP machines

To use this script you will need a list of hostnames you want to query. For this I run a simple net view and copy the hostnames from the command prompt window WITHOUT the \\. Paste the hostnames into notepad and lable it hosts.txt.

Next, copy the following into notepad

‘List the operating system and service pack of all computers in a text file.
On Error Resume Next

‘Initialize constants and variables.
strInputFile = "hosts.txt"
strOutputFile = "xpsp.txt"
g_strSP2 = ""
g_strSP1 = ""
g_strSP0 = ""
g_intSP2 = 0
g_intSP1 = 0
g_intSP0 = 0
g_intNotXP = 0
g_intErr = 0

‘Get list of computers from text file.
arrComputers = ReadTextFile(strInputFile)
For Each strComputer In arrComputers
  intGetSP = GetSP(strComputer)
  If 1 = intGetSP Then
    g_intErr = g_intErr + 1
  End If


WScript.Echo "Data written to " & strOutputFile


‘Read text file line by line and return array of lines.
Function ReadTextFile(strFileName)

On Error Resume Next

Dim arrLines()

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilename) Then
  Set objTextStream = objFSO.OpenTextFile(strFilename, FOR_READING)
  WScript.Echo "Input text file " & strFilename & " not found."
End If
Do Until objTextStream.AtEndOfStream
  intLineNo = objTextStream.Line
  ReDim Preserve arrLines(intLineNo – 1)
  arrLines(intLineNo – 1) = objTextStream.ReadLine
ReadTextFile = arrLines

End Function


‘Get list and count of computers by OS and SP.
Function GetSP(strComp)

On Error Resume Next

‘Connect to WMI on remote computer.
Set objWMIService = GetObject("winmgmts:\\" & strComp)
If Err <> 0 Then
  WScript.Echo "  Unable to connect to WMI."
  WScript.Echo "    Error Number:" & Err.Number
  WScript.Echo "    Source:" & Err.Source
  WScript.Echo "    Description:" & Err.Description
  GetSP = 1
  Exit Function
End If
Set colOSes = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_OperatingSystem")
For Each objOS in colOSes
  If objOS.Version = "5.1.2600" Then
    If objOS.ServicePackMajorVersion = "2" Then
      g_strSP2 = g_strSP2 & strComp & vbCrLf
      g_intSP2 = g_intSP2 + 1
    ElseIf objOS.ServicePackMajorVersion = "1" Then
      g_strSP1 = g_strSP1 & strComp & vbCrLf
      g_intSP1 = g_intSP1 + 1
      g_strSP0 = g_strSP0 & strComp & vbCrLf
      g_intSP0 = g_intSP0 + 1
    End If
    GetSP = 0
    g_intNotXP = g_intNotXP + 1
  End If
GetSP = 0

End Function


‘Write or append data to text file.
Sub WriteTextFile(strFileName)

On Error Resume Next

‘Open text file for output.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFileName) Then
  Set objTextStream = objFSO.OpenTextFile(strFileName, FOR_APPENDING)
  Set objTextStream = objFSO.CreateTextFile(strFileName)
End If

‘Write data to file.
objTextStream.WriteLine "Inventory of Windows XP Service Packs"
objTextStream.WriteLine "Taken " & Now
objTextStream.WriteLine vbCrLf & "Computers Running Windows XP"
objTextStream.WriteLine "============================"
objTextStream.WriteLine  "Total number: " & (g_intSP2 + g_intSP1 + g_intSP0)

objTextStream.WriteLine  vbCrLf & "Service Pack 2"
objTextStream.WriteLine  "————–"
objTextStream.WriteLine  g_strSP2
objTextStream.WriteLine  "Total number: " & g_intSP2

objTextStream.WriteLine  vbCrLf & "Service Pack 1"
objTextStream.WriteLine  "————–"
objTextStream.WriteLine  g_strSP1
objTextStream.WriteLine  "Total number: " & g_intSP1

objTextStream.WriteLine  vbCrLf & "No Service Pack"
objTextStream.WriteLine  "—————"
objTextStream.WriteLine  g_strSP0
objTextStream.WriteLine  "Total number: " & g_intSP0

objTextStream.WriteLine  vbCrLf & "Computers Not Running Windows XP"
objTextStream.WriteLine  "================================"
objTextStream.WriteLine  "Total number: " & g_intNotXP

objTextStream.WriteLine  vbCrLf & "Could Not Connect To Computer"
objTextStream.WriteLine  "============================="
objTextStream.WriteLine  "Total number: " & g_intErr


End Sub


Save your file as XP_Versions.vbs

Ensure that both hosts.txt and XP_Versions.vbs are in the same directory and simply run the XP_Versions.vbs. Your output will be in the same folder as the other 2 files and will be called xpsp.txt. The output will read similar to this:


Inventory of Windows XP Service Packs
Taken 2/4/2006 12:17:09 AM

Computers Running Windows XP
Total number: 2

Service Pack 2

Total number: 2

Service Pack 1

Total number: 0

No Service Pack

Total number: 0

Computers Not Running Windows XP
Total number: 1

Could Not Connect To Computer
Total number: 0


As you can see this script does not identify what the other machines are running and what machines are running other operating systems, it just identifies which XP computers need a service pack upgrage.

This information is provided "AS IS" with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *