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
}
2
  • Are you using PowerShell 2.0 or PowerShell 1.0 ? Commented Jun 7, 2013 at 3:44
  • I would have recommened Quest's AD cmdlets, they work the best for me. Commented Jun 7, 2013 at 10:38

2 Answers 2

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","[email protected]","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()
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.