How can I output in a table format the list of sites and the subnets in the site?

A. I combined the scripts from the FAQs "How can I use a script to list sites in Active Directory (AD)?" and "How can I list all subnets in a site from a script?" to create a script that outputs in HTML and to the screen a list of sites and the subnets linked to them. The parameter passed is the name of the file to output to. You can download the code here.

' listsiteswithsubnets.vbs
' John Savill
' Usage : listsiteswithsubnets.vbs 
' e.g. cscript listsiteswithsubnets.vbs c:\temp\sites.html

on error resume next

if WScript.Arguments.count > 0 then
	strFileName = WScript.Arguments(0)
	WScript.Echo "Will output to file " & strFileName & vbCRLF
	intOutputToFile = 1
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(strFileName, 2, True, 0)
	objFile.WriteLine "<html><head><title>Sites</title></head><body><table border=""1"" width=""100%"" id=""table1"">"
	objFile.WriteLine "<tr><td bgcolor=""#000000""><font color=""#FFFFFF"">Site</font></td><td bgcolor=""#000000""><font color=""#FFFFFF"">IP Subnets</font></td>"

else
	WScript.Echo "No output to file" & vbCRLF
	intOutputToFile = 0
end if

Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get("configurationNamingContext")
 
strSitesContainer = "LDAP://cn=Sites," & strConfigurationNC
Set objSitesContainer = GetObject(strSitesContainer)
objSitesContainer.Filter = Array("site")
 
For Each objSite In objSitesContainer
	WScript.Echo "Site: " & Mid(objSite.Name,4)
	objSite.GetInfoEx Array("siteObjectBL"), 0

	arrSiteObjectBL = objSite.GetEx("siteObjectBL")  'Need to check if this works
	If Err.number <> 0 Then
		WScript.Echo vbTab & "No Sites linked"
		Err = 0
		if intOutputToFile then
			objFile.WriteLine "<tr><td>" & Mid(objSite.Name,4) & "</td><td>No Sites</td></tr>"
		end if
	Else
		For Each strSiteObjectBL In arrSiteObjectBL
			WScript.Echo vbTab & Split(Split(strSiteObjectBL, ",")(0), "=")(1)
			if intOutputToFile then
				objFile.WriteLine "<tr><td>" & Mid(objSite.Name,4) & "</td><td>" & Split(Split(strSiteObjectBL, ",")(0), "=")(1) & "</td></tr>"
			end if
		Next	
	End If

Next

if intOutputToFile then
	objFile.WriteLine "</table></body></html>"
	objFile.Close
end if

The sample command execution, D:\Temp>cscript listsiteswithsubnets.vbs test.html will output to the file test.html and the following information will output to screen.

Site: Boston
    192.168.3.0/24
Site: Dallas
    192.168.2.0/24
    192.168.1.0/24
Site: London
    192.168.1.248/29
Site: NewYork
    No Sites linked
Site: Smallville
    192.168.4.0/24

The figure shows the HTML table output.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish