Solved

How to list all Windows server in a specific subnet?

Posted on 2011-02-14
5
580 Views
Last Modified: 2012-08-14
How to list all Windows servers in a specific subnet?
0
Comment
Question by:SAM2009
5 Comments
 
LVL 6

Expert Comment

by:ashunnag
ID: 34887566
you need a tool for ping sweep like R3X. you can download it from this link:
http://www.4shared.com/file/I23FIfhy/Project_R3x_v_060_SE.html
0
 
LVL 11

Expert Comment

by:kaskhedikar_tushar
ID: 34887644
0
 
LVL 5

Expert Comment

by:NotVeryFat
ID: 34887904
Just to add to the above, a very simple free tool is AngryIP (www.angryip.org) that can scan by IP address range/ subnet and export to csv etc.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34892362
Hi there, this VBS will do the job.  It reads the servers names from Active Directory, and lists them in a CSV file in subnet order.

Regards,

Rob.
'http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0202.mspx
Const ADS_SCOPE_SUBTREE = 2

Set objShell = CreateObject("WScript.Shell")

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select name, operatingSystem, operatingSystemVersion from 'LDAP://" & strDNSDomain & "' where objectClass='computer' ORDER BY operatingSystem"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Const adVarChar = 200
Const MaxCharacters = 255
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "Subnet", adVarChar, MaxCharacters
DataList.Fields.Append "ServerName", adVarChar, MaxCharacters
DataList.Fields.Append "OperatingSystem", adVarChar, MaxCharacters
DataList.Fields.Append "OperatingSystemVersion", adVarChar, MaxCharacters
DataList.Open

strCSVFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "ServersAndSubnets.csv"
Do Until objRecordSet.EOF
	If InStr(LCase(objRecordSet.Fields("operatingSystem").Value), "server") > 0 Then
		strServer = objRecordSet.Fields("name").Value
		strIP = ResolveIP(strServer)
		strOS = objRecordSet.Fields("operatingSystem").Value
		strOSVersion = objRecordSet.Fields("operatingSystemVersion").Value
		DataList.AddNew
		If InStr(strIP, ".") > 0 Then
			DataList("Subnet") = Left(strIP, InStrRev(strIP, ".") - 1)
		Else
			DataList("Subnet") = strIP
		End If
		DataList("ServerName") = strServer
		DataList("OperatingSystem") = strOS
		DataList("OperatingSystemVersion") = strOSVersion
		DataList.Update
	End If
	objRecordSet.MoveNext
Loop

DataList.Sort = "Subnet,ServerName"
DataList.MoveFirst

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile(strCSVFile, True)
objOutput.WriteLine """Subnet"",""Server Name"",""Operating System"",""Operating System Version"""
While Not DataList.EOF
	objOutput.WriteLine """" & DataList("Subnet") & """,""" & DataList("ServerName") & """,""" & DataList("OperatingSystem") & """,""" & DataList("OperatingSystemVersion") & """"
	DataList.MoveNext
Wend
DataList.Close
objOutput.Close

MsgBox "Done. Please see " & strCSVFile

Function ResolveIP(computerName)
	Set objShell = CreateObject("WScript.Shell")
	Set objExec = objShell.Exec("ping " & computerName & " -n 1")
	strOutput = objExec.StdOut.ReadAll
	Set RegEx = New RegExp
	RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
	RegEx.Global = True
	If RegEx.Test(strOutput) Then
		ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
	Else
		ResolveIP = "IP Address could not be resolved"
	End If
End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:SAM2009
ID: 34903775
Very good script! Thanks again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now