How to list all  computers in a given active directory site

Posted on 2009-04-14
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?

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!


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 20 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Here's a look at newsworthy articles and community happenings during the last month.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

707 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