/ Published in: Visual Basic
This script, building on my AllWindowsCrawler snippet, records DNS server entries (up to 3) as well as the IP Address and AD site location for a given Server (the AllWindows portion is commented out). This query is filtered to select only IP enabled adapters with DHCP disabled (i.e. static IP). The results are written to a CSV file.
Expand |
Embed | Plain Text
On Error Resume Next Call Payload() WScript.Echo "Done" '***************************************************************************************************************************************************** Function Payload() Set ofsout = CreateObject("Scripting.FileSystemObject") Set readout = ofsout.OpenTextFile("C:\VB Script\" & OutPutFileName("","csv"),2,true) readout.writeline "Server Name,AD Site Name,IP Address,DNS Server 1,DNS Server 2,DNS Server 3," On Error Resume Next Const wbemFlagReturnImmediately=&h10 Const wbemFlagForwardOnly=&h20 Const ADS_SCOPE_SUBTREE = 2 ' Determine DNS domain name from RootDSE object. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("defaultNamingContext") ' Use ADO to search Active Directory for all computers. Set adoCommand = CreateObject("ADODB.Command") Set adoConnection = CreateObject("ADODB.Connection") adoConnection.Provider = "ADsDSOObject" adoConnection.Open "Active Directory Provider" adoCommand.ActiveConnection = adoConnection ' Search entire domain. strBase = "<LDAP://" & strDNSDomain & ">" ' Filter on computer objects with server operating system. ' Uncomment this line to get all computers in a domain 'strFilter = "(&(objectCategory=computer))" strFilter = "(&(objectCategory=computer)(operatingSystem=*server*))" ' Comma delimited list of attribute values to retrieve. strAttributes = "cn" ' Construct the LDAP syntax query. strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" adoCommand.CommandText = strQuery adoCommand.Properties("Page Size") = 1000 adoCommand.Properties("Timeout") = 30 adoCommand.Properties("Cache Results") = False adoCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE Set adoRecordset = adoCommand.Execute ' Enumerate computer objects with server operating systems. Do Until adoRecordset.EOF strComputer = adoRecordset.Fields("cn").Value WScript.Echo "Checking " & strComputer If IsHostAlive(strComputer) = True Then Wscript.Echo "Processing: " & strComputer Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("SELECT DNSServerSearchOrder, Description, IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True AND DHCPEnabled = false") For Each objItem in colItems DNSConfiguration = Join(objItem.DNSServerSearchOrder, ",") IPAddress = objItem.IPAddress(0) strSiteName = SiteName(IPAddress) if LEN(DNSConfiguration)>1 THEN wscript.echo "Site Name: " & SiteName & vbcrlf 'wscript.echo strComputer & "," & objItem.IPAddress(0) & "," & DNSConfiguration 'readout.writeline strComputer & "," & objItem.IPAddress(0) & "," & DNSConfiguration & "," ' & vbcrlf wscript.echo "Computer Name: " & strComputer & vbCrLf & "Site Name: " & strSiteName & vbCrLf & "IpAddresses: " & IPAddress & vbCrLf & "DNS Servers: " & DNSConfiguration readout.writeline strComputer & "," & strSiteName & "," & IPAddress & "," & DNSConfiguration & "," ' & vbcrlf end if Next 'Close queries Set objWMIService = Nothing Set colItems = Nothing Else wscript.echo "ERROR: " & strComputer & " cannot be reached or the account doesn't have proper permissions" End If 'End IF adoRecordset.MoveNext Loop ' Clean up. adoRecordset.Close adoConnection.Close 'Close Files readout.close() 'Function Complete End Function '***************************************************************************************************************************************************** Function SiteName(IPAddr) '------------------------------------------------------------------------------' ' Displays the AD Site name for the supplied dotted decimal IP address. ' http://www.pcreview.co.uk/forums/determine-active-directory-site-given-ip-address-t1447992.html '------------------------------------------------------------------------------' ' IP2Site.vbs witten April 4, 2003 by Wayne Tilton '------------------------------------------------------------------------------' Dim ConfigNameContext Dim DecIPAddr, MaskBits, NumAddrs, LoIPAddr, HiIPAddr, SiteAddrs Dim SubnetContainer, LDAPQry, Subnet Dim AdConn, AdComm, AdRS SiteName = "Default-First-Site-Name" ' Default site name if no matches DecIPAddr = Dot2Dec(IPAddr) ' Convert supplied address to decimal ConfigNameContext = GetObject("LDAP://RootDSE").Get("configurationNamingContext") SubnetContainer = "CN=Subnets,CN=Sites," & ConfigNameContext LDAPQry = "<LDAP://" & SubnetContainer & ">;(objectCategory=subnet);cn,siteObject" Set AdConn = CreateObject("ADODB.Connection") ' Get an ADO connection object AdConn.Provider = "ADsDSOObject" ' Set provider name AdConn.Open "Active Directory Provider" ' open connection Set AdComm = CreateObject("ADODB.Command") ' Get an ADO command object Set AdComm.ActiveConnection = AdConn ' Tell command object about connection AdComm.Properties("SearchScope") = 2 ' we want to search everything AdComm.Properties("Page Size") = 500 ' and we want our records in lots of 500 AdComm.CommandText = LDAPQry ' Set the ADOCommandText Set AdRS = AdComm.Execute ' and run the query. SiteAddrs = (2^31)-1 ' Really big range AdRS.MoveFirst ' Go to 1st record in the set While Not AdRS.EOF ' Read 'em until they're gone Subnet = AdRs.Fields("cn") ' Get subnet name MaskBits = Split(Subnet,"/") ' Split IP addr from subnet mask NumAddrs = (2 ^ (32 - MaskBits(1))) - 1 ' Calc # addresses based on subnet mask LoIPAddr = Dot2Dec(MaskBits(0)) ' Get low end of IP range in decimal HiIPAddr = LoIPAddr + NumAddrs ' Calc high end of range by adding # add ' Check to see if IP is within this subnet range and if so that it's smallest subnet with that addr If DecIPAddr => LoIPAddr And DecIPAddr <= HiIPAddr And NumAddrs <= SiteAddrs then SiteName = AdRs.Fields("siteObject") SiteName = GetObject("LDAP://" & AdRs.Fields("siteObject")).Get("name") Wscript.echo "Sitename: " & sitename SiteAddrs = NumAddrs ' Remember # addrs in this subnet End If AdRS.MoveNext ' Go to next record or EOF Wend Set AdRs = Nothing Set AdComm = Nothing Set AdConn = Nothing End Function Function IsHostAlive(strComputer) IsHostAlive = False ' normally we will return false, unless ping is successful Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" + strComputer + "'") For Each oRow In colItems Select Case oRow.StatusCode Case 0 IsHostAlive = True wscript.echo strComputer & " is alive..." Case 11001 wscript.echo "Buffer Too Small" Case 11002 wscript.echo "Destination Net Unreachable" Case 11003 wscript.echo "Destination Host Unreachable" Case 11004 wscript.echo "Destination Protocol Unreachable" Case 11005 wscript.echo "Destination Port Unreachable" Case 11006 wscript.echo "No Resources" Case 11007 wscript.echo "Bad Option" Case 11008 wscript.echo "Hardware Error" Case 11009 wscript.echo "Packet Too Big" Case 11010 wscript.echo "Request Timed Out" Case 11011 wscript.echo "Bad Request" Case 11012 wscript.echo "Bad Route" Case 11013 wscript.echo "TimeToLive Expired Transit" Case 11014 wscript.echo "TimeToLive Expired Reassembly" Case 11015 wscript.echo "Parameter Problem" Case 11016 wscript.echo "Source Quench" Case 11017 wscript.echo "Option Too Big" Case 11018 wscript.echo "Bad Destination" Case 11032 wscript.echo "Negotiating IPSEC" Case 11050 wscript.echo "General Failure" Case Else wscript.echo "Status code " & objPing.StatusCode & " - Unable to determine cause of failure." End Select Next End Function Function OutPutFileName(ScriptFileName, fileEXT) strFileDate = Year(Now) & pd(Month(Now),2) & pd(Day(Now),2) & "-" & pd(Hour(Now),2) & pd(Minute(Now),2) & pd(Second(Now),2) If ScriptFileName = "" Then ScriptFileName = Left(WScript.ScriptName, Len(WScript.ScriptName) - 4) End If OutPutFileName = ScriptFileName & "_" & strFileDate & "." & fileEXT End Function ' Returns the decimal value of a dotted decimal IP address Function Dot2Dec(IPAddress) Dim Octets Octets = Split(IPAddress,".") Dot2Dec = (Octets(0)*(2^24)) + (Octets(1)*(2^16)) + (Octets(2)*(2^8)) + Octets(3) End Function 'Pad single digit numbers with leading zero Function pd(n, totalDigits) if totalDigits > len(n) then pd = String(totalDigits-len(n),"0") & n else pd = n end if End Function '*****************************************************************************************************************************************************
You need to login to post a comment.
