Solved

How to list all  computers in a given active directory site

Posted on 2009-04-14
7
262 Views
Last Modified: 2012-05-06
Is there an easy way of showing all the computers currently in each active directory site within a domain ?
0
Comment
Question by:drlargepants
  • 4
  • 3
7 Comments
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

It's not amazingly easy, you have to grab the list of subnets and do a bit of IP math to figure out if each computer is part of that subnet (typically based on a lookup of a name in DNS).

You have a lot of them?

Chris
0
 

Author Comment

by:drlargepants
Comment Utility
A couple of 100 ?!  Its from an NT 4 migration, so ideally we need to know what is where and then create an OU for each site.
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Do you have a list of your IP ranges (network address and subnet mask)? And a list of the computers?

Chris
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:drlargepants
Comment Utility
Yeah can get that easy enough from angryip
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Then it's not so hard to do, I have a pile of VbScript subroutines for doing IP math and have done this kind of operation before for a much bigger domain.

It's reliant on being able to resolve the computer names to IP addresses, but I guess your infrastructure already allows that?

Chris
0
 

Author Comment

by:drlargepants
Comment Utility
yeah DNS is good to go :)
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 20 total points
Comment Utility

Cool, in that case we can have a play with this one. I've based this on files looking like this:

subnets.txt

192.168.2.0/24
10.45.0.0/23
172.16.0.0/22

computers.txt

computer1
computer2
computer3

It is possible to read the subnets from Active Directory instead of a file if they're configured there and you'd rather use those.

The file it creates a is a CSV file, you should be able to sort it by the Network Address field (in Excel) which will give you a break down of PCs within a given site. If we read subnets from AD we can add the site name as well.

Chris


Option Explicit
 

Const SUBNET_LIST = "subnets.txt"

Const COMPUTER_LIST = "computers.txt"

Const REPORT_FILE = "report.csv"
 

Function GetDNSAddresses(strComputerName)

  ' Return Type: Array

  '

  ' Returns an Array containing each IP Address for a given Node Name from a

  ' DNS Query
 

  Dim objShell : Set objShell = CreateObject("WScript.Shell")

  Dim objScriptExec : Set objScriptExec = objShell.Exec("nslookup " & strComputerName)

  Dim objStdOut : Set objStdOut = objScriptExec.StdOut

	

  Dim strIPAddress : strIPAddress = ""

  Do Until objStdOut.AtEndOfStream

    Dim strOut : strOut = objStdOut.ReadLine

    If InStr(strOut, "Name:") Then

      strIPAddress = objStdOut.ReadLine

	

      If InStr(strIPAddress, "Address:") > 0 Then

        strIPAddress = Trim(Replace(strIPAddress, "Address:", ""))

      ElseIf InStr(strIPAddress, "Addresses:") > 0 Then

        strIPAddress = Trim(Replace(strIPAddress, "Addresses:", ""))

      End If

    End If

  Loop

	

  GetDNSAddresses = Split(strIPAddress, ", ")

	

  Set objStdOut = Nothing

  Set objScriptExec = Nothing

  Set objShell = Nothing

End Function
 

Function ConvertIPToBinary(strIP)

  ' Converts an IP Address into Binary

 

  Dim arrOctets : arrOctets = Split(strIP, ".")

  Dim i

  For i = 0 to UBound(arrOctets)

    Dim intOctet : intOctet = CInt(arrOctets(i))

    Dim strBinOctet : strBinOctet = ""

    Dim j

    For j = 0 To 7

      If intOctet And (2^(7 - j)) Then

        strBinOctet = strBinOctet & "1"

      Else

        strBinOctet = strBinOctet & "0"

      End If

    Next

    arrOctets(i) = strBinOctet

  Next

  ConvertIPToBinary = Join(arrOctets, ".")

End Function
 

Function ConvertBinIPToDecimal(strBinIP)

  ' Convert binary form of an IP back to decimal
 

  Dim arrOctets : arrOctets = Split(strBinIP, ".")

  Dim i

  For i = 0 to UBound(arrOctets)

    Dim intOctet : intOctet = 0

    Dim j

    For j = 0 to 7

      Dim intBit : intBit = CInt(Mid(arrOctets(i), j + 1, 1))

      If intBit = 1 Then

        intOctet = intOctet + 2^(7 - j)

      End If

    Next

    arrOctets(i) = CStr(intOctet)

  Next

 

  ConvertBinIPToDecimal = Join(arrOctets, ".")

End Function
 

Function MaskLengthToIP(intMask)

  ' Converts a mask length to the decimal format mask
 

  Dim intFullOctets : intFullOctets = (intMask - (intMask Mod 8)) / 8

  Dim i : Dim arrOctets(3)

  For i = 0 To (intFullOctets - 1)

    arrOctets(i) = "255"

  Next

 

  Dim intPartialOctetLen : intPartialOctetLen = intMask Mod 8

  Dim j, intOctet

  If intPartialOctetLen > 0 Then

    For j = 0 To (intPartialOctetLen - 1)

      intOctet = intOctet + 2^(7 - j)

    Next

    arrOctets(i) = intOctet : i = i + 1

  End If

 

  For j = i To 3

    arrOctets(j) = "0"

  Next

 

  MaskLengthToIP = Join(arrOctets, ".")

End Function
 

Function CalcNetworkAddress(strIP, strMask)

  ' Generates the Network Address from the IP and Mask
 

  ' Conversion of IP and Mask to binary

  Dim strBinIP : strBinIP = ConvertIPToBinary(strIP)

  Dim strBinMask : strBinMask = ConvertIPToBinary(strMask)

 

  ' Bitwise AND operation (except for the dot)

  Dim i, strBinNetwork

  For i = 1 to Len(strBinIP)

    Dim strIPBit : strIPBit = Mid(strBinIP, i, 1)

    Dim strMaskBit : strMaskBit = Mid(strBinMask, i, 1)

 

    If strIPBit = "1" And strMaskBit = "1" Then

      strBinNetwork = strBinNetwork & "1"

    ElseIf strIPBit = "." Then

      strBinNetwork = strBinNetwork & strIPBit

    Else

      strBinNetwork = strBinNetwork & "0"

    End If

  Next

 

  ' Conversion of Binary IP to Decimal

  CalcNetworkAddress= ConvertBinIPToDecimal(strBinNetwork)

End Function
 
 

'

' Main Code

'
 

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
 

Dim objSubnetFile : Set objSubnetFile = objFSO.OpenTextFile(SUBNET_LIST, 1, False, 0)

Dim objSubnetList : Set objSubnetList = CreateObject("Scripting.Dictionary")
 

Do While Not objSubnetFile.AtEndOfStream

  Dim arrTemp : arrTemp = Split(objSubnetFile.ReadLine, "/")

  Dim strNetworkAddress : strNetworkAddress = arrTemp(0)

  Dim strMaskLength : strMaskLength = arrTemp(1)
 

  objSubnetList.Add strNetworkAddress, strMaskLength

Loop
 

Set objSubnetFile = Nothing
 

Dim objComputerFile : Set objComputerFile = objFSO.OpenTextFile(COMPUTER_LIST, 1, False, 0)

Dim objReportFile : Set objReportFile = objFSO.OpenTextFile(REPORT_FILE, 2, True, 0)

objReportFile.WriteLine "Client Name,Client IP,Network Address,Subnet Mask"
 

Do While Not objComputerFile.AtEndOfStream

  Dim strClientName : strClientName = objComputerFile.ReadLine

  Dim arrClientIPs : arrClientIPs = GetDNSAddresses(strClientName)
 

  Dim strClientIP

  For Each strClientIP in arrClientIPs
 

    For Each strNetworkAddress in objSubnetList

      Dim strIPMask : strIPMask = MaskLengthToIP(objSubnetList(strNetworkAddress))

      If strNetworkAddress = CalcNetworkAddress(strClientIP, strIPMask) Then

        objReportFile.WriteLine strClientName & "," & strClientIP & "," & strNetworkAddress & "," & strIPMask

        Exit For

      End If

    Next

  Next

Loop

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

I'm sure that every Windows systems administrator has written, or at least used, a batch or VBS login script at some point in their career, whether it is to map network drives, install printers, or set some user preferences.  No more! With Window…
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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 …

772 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

12 Experts available now in Live!

Get 1:1 Help Now