0

So at the moment I have some code which can lock down to a specific OU via ADSI in Powershell, loop through, and store them into an Array. In turn I loop through this and run a Test-Connection. I have my reasons...

Anyway, is it possible (using only inbuilt cmdlets, i.e. no Quest stuff) to recurse through the whole of AD and add all Computer Objects to the array?

$myArrayOfComputers = @()

$orgUnit = [ADSI]"LDAP://OU=foo,DC=foo,dc=co,dc=uk"

ForEach($child in $orgUnit.psbase.Children) {
    if ($child.ObjectCategory -like '*computer*') { $myArrayOfComputers += $child.Name }
}

ForEach($i in $myArrayOfComputers) {
    Test-Connection $i
}
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
PnP
  • 3,133
  • 17
  • 62
  • 95

2 Answers2

1

In PowerShell V2.0 you can try :

Import-module ActiveDirectory
$computers = Get-ADComputer *

In PowerShell V1.0 You can try :

# dom.fr is the DNS root name of the domain
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://dom.fr:389/dc=dom,dc=fr","administrator@dom.fr","admin")

# Look for computers
$Rech = new-object System.DirectoryServices.DirectorySearcher($dn)
$Rech.filter = "((objectClass=computer))"
$Rech.SearchScope = "subtree"
$Rech.PropertiesToLoad.Add("sAMAccountName");  
$Rech.PropertiesToLoad.Add("lastLogon");  
$Rech.PropertiesToLoad.Add("distinguishedname");

$computers = $Rech.findall()
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
1

On V2 using .net:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement | out-null
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = new-object  'System.DirectoryServices.AccountManagement.PrincipalContext'($ct, "foo.co.uk", "OU=foo,DC=foo,dc=co,dc=uk");
$cpp = New-Object 'System.DirectoryServices.AccountManagement.Computerprincipal'($pc)
$ps = new-object  'System.DirectoryServices.AccountManagement.PrincipalSearcher'
$ps.QueryFilter = $cpp
$MyListArray = $ps.FindAll() | select -expa name
CB.
  • 58,865
  • 9
  • 159
  • 159