Powershell -将AD中的所有计算机存储到数组中



因此,目前我有一些代码可以通过Powershell中的ADSI锁定到特定的OU,循环并将它们存储到数组中。然后,我循环遍历并运行Test-Connection。我有我的理由……

无论如何,是否有可能(仅使用内置的cmdlet,即没有任务的东西)递归通过整个AD并将所有计算机对象添加到数组?

$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
}

在PowerShell V2.0中,您可以尝试:

Import-module ActiveDirectory
$computers = Get-ADComputer *

在PowerShell V1.0中你可以尝试:

# 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()

使用。net的V2:

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

相关内容

最新更新