This script connects to Active Directory and writes all the servers that are running Microsoft Windows Server 2003 to a text file; the next script uses this information as an input to create a computer rule element in ISA Server 2004.
'==================================================================== ' ' PURPOSE: Add all the Computers running Windows Server 2003 from ' Active Directory to a text file called c:\ExportedServers.txt ' ' NAME: ExportServers.vbs ' ' RUN: cscript ExportServers.vbs <Domain Controller> ' ' WHERE: Run on a domain member of Active Directory as an administrator 'of the local computer, which must be Windows Server 2003. ' ' COMMENT: The script will only return Windows Server 2003 computers. ' This script was written by Jesper Hanno Hansen. ' ' VERSION: 1.0 '==================================================================== 'Set ForAppending from OpenAsTextStream Method Const ForAppending = 8 ' Create an arguments Object to get the parameters from the input Dim objArguments Set objArguments = WScript.Arguments ' Count the input parameters, if less than 1, show help If objArguments.Count < 1 Then ShowHelp Else ' Set the parameter to the Computername Dim strServerName strServerName = WScript.Arguments(0) End If' ' Calling the Sub to connect to Active Directory FindServers strServerName WScript.Echo "Please check the file C:\ExportedServers.txt" WScript.Echo "to verify the data..." Sub FindServers(Server) ' Show information on the screen WScript.Echo "Connecting to server: " & strServerName WScript.Echo "" ' Create a Root Object of the LDAP namespace Dim objRootSet objRoot = GetObject("LDAP:") ' Declare variables Dim objADsPath Dim strADsPath ' Looping through available ADsPath For Each objADsPath In objRoot ' Set AdsPathstrADsPath = objADsPath.AdsPath Next ' Create a Connection Object Dim objConnection Set objConnection = CreateObject("ADODB.Connection") ' Set Provider Property to ADSI OLE DB Provider objConnection.Provider = "ADsDSOObject" ' Open the Connection to Active Directory objConnection.Open "ADs Provider" ' Create a RecordSet Object Dim objRecordset Set objRecordSet = CreateObject("ADODB.Recordset") ' Set the Query we will use against Active Directory ' Note the filter operatingSystem=Windows Server 2003 define ' that the query will return the Windows Server 2003 only Dim strQuery strQuery = "<" & strADsPath & ">" & _ ";(&(objectClass=Computer)(operatingSystem=Windows Server 2003))" & _ ";DNSHostName,description,distinguishedName;SubTree" ' Set the RecordSet Object to the result of the executed query Set objRecordSet = objConnection.Execute(strQuery) ' Declare variables Dim strDNSHostname, strDescription, strIPAddress, strDistinguishedName ' Loop through the RecordSet, until End Of File Do Until objRecordset.EOF ' Enable Error Handling On Error Resume Next ' Set the results to variables strDNSHostname = objRecordSet.Fields("dNSHostName") WScript.Echo "Found : " & strDNSHostname CheckError strDistinguishedName = objRecordset.Fields("distinguishedName") CheckError ' Since Active Directory does not have the IP Address of a ' computer, we will ping the FQDN using a WMI Function and ' set the result as strIPAddress strIPAddress = GetIPAddress(strDNSHostname) CheckError ' The Description field in Active Directory is not returned using ' our query, so a new connection directly to the DistinguishedName ' of the Server is required ' Create an ADsGetObject Object to get access to the Description ' property Set objADsGetObject = GetObject("LDAP://" & strDistinguishedName) CheckError ' Set Description variable to the value of the Description field strDescription = objADsGetObject.Get("Description") ' Check if the value is empty, if true clear the error If IsNull(strDescription) Then Err.Clear End If ' Show information on the screen WScript.Echo "Information : " & strDNSHostname & " " & _ strIPAddress & " " & strDescription Wscript.Echo "" ' Send the values to the write sub WriteToFile strDNSHostName, strIPAddress, strDescription ' Clear the description strDescription = Null ' Move to the next record in the recordset objRecordset.MoveNext Loop End Sub Sub WriteToFile(strDNSHostName, strIPAddress, strDescription) ' Create a FileSystem Object Dim objFileSystemObject Set objFileSystemObject = CreateObject("Scripting.FileSystemObject") ' Create a File Object, and open/create a file named ExportedServers.txt ' if the file exists, append data to it, if not create a new file. Dim objFile Set objFile = objFileSystemObject.OpenTextFile _ ("C:\ExportedServers.txt", ForAppending,True) ' Write the data to the file objFile.WriteLine strDNSHostname & "|" & strIPAddress & "|" & _ strDescription ' Close the file again objFile.Close End Sub Function GetIPAddress(strServer) On Error Resume Next ' Create a sWbemLocator Object to get access to sWbemService Dim objsWbemLocator Set objsWbemLocator = CreateObject("WbemScripting.SWbemLocator") CheckError ' Create a sWbemService object to get access to sWbemObjectSet Dim objsWbemService Set objsWbemService = objsWbemLocator.ConnectServer(".","root\CIMV2") CheckError ' Create a sWbemObjectSet using the ExecQuery method of a ping query Dim objsWbemObjectSet Set objsWbemObjectSet = objsWbemService.ExecQuery("select * from " & _ "Win32_PingStatus where address = '" & strServer & "'") CheckError ' Declare Dim objsWbemObject ' Loop through the query For Each objSwbemObject in objsWbemObjectSet ' If statuscode is null or <> 0 there was an error, else ok If IsNull(objsWbemObject.StatusCode) Or _ objsWbemObject.StatusCode <> 0 Then GetIPAddress = "" Else ' Set GetIPAddress to Address of the destination GetIPAddress = objsWbemObject.ProtocolAddress End If Next End Function ' Used to check if the script is in an error state, if true quit Sub CheckError If Err.Number <> 0 Then WScript.Echo "" WScript.Echo "Error Number : " & "0x" & Hex(Err.Number) WScript.Echo "Error Description : " & Err.Description WScript.Quit End If End Sub ' Used to show help on the screen, if input parameters is less than 1 Sub ShowHelp WScript.Echo "Please use cscript ExportServers.vbs <Domain Controller>" WScript.Echo "For Example:" WScript.Echo "" WScript.Echo "cscript ExportServers.vbs Server01.domain.local" WScript.Quit End Sub
Note | This function can also be performed using one of the ds commands included with Windows Server 2003. This will take one line rather than 211. From a command line, type dsquery * -filter "(&(objectCategory=computer)(operatingSystem=Windows Server 2003)) -attr name - l >c:\ExportedServers.txt. |