Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

How to list all computers in a given active directory site

Is there an easy way of showing all the computers currently in each active directory site within a domain ?
0
drlargepants
Asked:
drlargepants
  • 4
  • 3
1 Solution
 
Chris DentPowerShell DeveloperCommented:

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
 
drlargepantsAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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

Chris
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
drlargepantsAuthor Commented:
Yeah can get that easy enough from angryip
0
 
Chris DentPowerShell DeveloperCommented:

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
 
drlargepantsAuthor Commented:
yeah DNS is good to go :)
0
 
Chris DentPowerShell DeveloperCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now