VBScript to determine OS Version and Service Pack
Filed Under VBScript
The next few posts will all revolve around using vbscript as a solution to common tasks/needs while working in a Windows Server environment. These scripts were created to help make my job easier.
The first script I want to share demonstrates how to poll Active Directory and return all computers and their Operating System and service pack. My specific need was to find all computers not running XP Service Pack 3. The general idea for this script is to return what AD sees and place the output into a text file. This script is very easily modified to suit many needs and simply uses ADO and an LDAP query to get the results. It then uses FSO to place the results into a text file.
Option Explicit ' Declare some variables Dim strFilePath, objFSO, objFile, adoConnection, adoCommand Dim objRootDSE, strDNSDomain, strFilter, strQuery, adoRecordset Dim strDN, strComputerName, strOS, strOSVer Dim computerCount Dim OUArray, i, strOU ' Define the path for our text file strFilePath = "C:\log.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") ' Open the file for write access. Set objFile = objFSO.OpenTextFile(strFilePath, 2, True, 0) ' Use ADO to search the domain for all users. Set adoConnection = CreateObject("ADODB.Connection") Set adoCommand = CreateObject("ADODB.Command") adoConnection.Provider = "ADsDSOOBject" adoConnection.Open "Active Directory Provider" Set adoCommand.ActiveConnection = adoConnection ' Determine the DNS domain from the RootDSE object. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("DefaultNamingContext") ' Filter to retrieve all user objects. strFilter = "(&(objectCategory=computer)(objectClass=computer))" strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter _ & ";distinguishedName,operatingsystem,operatingsystemservicepack,samaccountname;subtree" adoCommand.CommandText = strQuery adoCommand.Properties("Page Size") = 100 adoCommand.Properties("Timeout") = 190 adoCommand.Properties("Cache Results") = False computerCount = 0 Set adoRecordset = adoCommand.Execute Do Until adoRecordset.EOF strComputerName = replace(adoRecordset.Fields("samaccountname").Value,"$","") strOS = adoRecordset.Fields("operatingsystem").Value strOSVer = adoRecordset.Fields("operatingsystemservicepack").Value strDN = adoRecordset.Fields("distinguishedName").Value OUArray = split(strDN,",") strOU = "" ' Filter out just the Organizational unit of the computer For i = 0 to ubound(ouarray) If instr(ouarray(i),"OU=") > 0 Then strOU = strOU & "," & ouarray(i) End If Next strOU = mid(strOU,2,len(strOU)) ' Determine if we find a computer that is running XP but does NOT have Service Pack 3 If strOS <> "" and strOSVer <> "" Then If strOS = "Windows XP Professional" and strOSVer <> "Service Pack 3" Then objFile.WriteLine strComputerName & " ; " & strOS & " ; " & strOSVer & " ; " & strOU computerCount = computerCount + 1 End If End If adoRecordset.MoveNext Loop ' Clean up. adoRecordset.Close objFile.Close adoConnection.Close Set objFile = Nothing Set objFSO = Nothing Set adoConnection = Nothing Set adoCommand = Nothing Set objRootDSE = Nothing Set adoRecordset = Nothing Wscript.Echo "Done" & " - " & computerCount & " Records Found"
The output file will generate results that is semicolon delimited and will look like:
Computer Name; Operating System; Service Pack; Organizational Unit