How can I output in a table format the list of sites and the subnets in the site?
John Savill
March 12, 2006
1 Min Read
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:tempsites.htmlon error resume nextif WScript.Arguments.count > 0 thenstrFileName = WScript.Arguments(0)WScript.Echo "Will output to file " & strFileName & vbCRLFintOutputToFile = 1Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile(strFileName, 2, True, 0)objFile.WriteLine ""objFile.WriteLine "SiteIP Subnets"elseWScript.Echo "No output to file" & vbCRLFintOutputToFile = 0end ifSet objRootDSE = GetObject("LDAP://RootDSE")strConfigurationNC = objRootDSE.Get("configurationNamingContext") strSitesContainer = "LDAP://cn=Sites," & strConfigurationNCSet objSitesContainer = GetObject(strSitesContainer)objSitesContainer.Filter = Array("site") For Each objSite In objSitesContainerWScript.Echo "Site: " & Mid(objSite.Name,4)objSite.GetInfoEx Array("siteObjectBL"), 0arrSiteObjectBL = objSite.GetEx("siteObjectBL") 'Need to check if this worksIf Err.number <> 0 ThenWScript.Echo vbTab & "No Sites linked"Err = 0if intOutputToFile thenobjFile.WriteLine "" & Mid(objSite.Name,4) & "No Sites"end ifElseFor Each strSiteObjectBL In arrSiteObjectBLWScript.Echo vbTab & Split(Split(strSiteObjectBL, ",")(0), "=")(1)if intOutputToFile thenobjFile.WriteLine "" & Mid(objSite.Name,4) & "" & Split(Split(strSiteObjectBL, ",")(0), "=")(1) & ""end ifNextEnd IfNextif intOutputToFile thenobjFile.WriteLine ""objFile.Closeend if
The sample command execution, D:Temp>cscript listsiteswithsubnets.vbs test.htmlwill output to the file test.html and the following information will output to screen.
Site: Boston 192.168.3.0/24Site: Dallas 192.168.2.0/24 192.168.1.0/24Site: London 192.168.1.248/29Site: NewYork No Sites linkedSite: Smallville 192.168.4.0/24
The figure shows the HTML table output.
About the Author
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
You May Also Like