How to list all  computers in a given active directory site

Posted on 2009-04-14
Medium Priority
Last Modified: 2012-05-06
Is there an easy way of showing all the computers currently in each active directory site within a domain ?
Question by:drlargepants
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 71

Expert Comment

by:Chris Dent
ID: 24139294

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?


Author Comment

ID: 24139336
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.
LVL 71

Expert Comment

by:Chris Dent
ID: 24139394

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.


Author Comment

ID: 24139531
Yeah can get that easy enough from angryip
LVL 71

Expert Comment

by:Chris Dent
ID: 24139578

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?


Author Comment

ID: 24139791
yeah DNS is good to go :)
LVL 71

Accepted Solution

Chris Dent earned 80 total points
ID: 24146379

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




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.


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
  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"
        strBinOctet = strBinOctet & "0"
      End If
    arrOctets(i) = strBinOctet
  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
    arrOctets(i) = CStr(intOctet)
  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"
  Dim intPartialOctetLen : intPartialOctetLen = intMask Mod 8
  Dim j, intOctet
  If intPartialOctetLen > 0 Then
    For j = 0 To (intPartialOctetLen - 1)
      intOctet = intOctet + 2^(7 - j)
    arrOctets(i) = intOctet : i = i + 1
  End If
  For j = i To 3
    arrOctets(j) = "0"
  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
      strBinNetwork = strBinNetwork & "0"
    End If
  ' 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
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

Open in new window


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
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 …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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