Solved

How to list all Windows server in a specific subnet?

Posted on 2011-02-14
5
572 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
Comment Utility
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
Comment Utility
0
 
LVL 5

Expert Comment

by:NotVeryFat
Comment Utility
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
Comment Utility
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
Comment Utility
Very good script! Thanks again!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

762 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

13 Experts available now in Live!

Get 1:1 Help Now