【问题标题】:Powershell - Store all Computers in AD into ArrayPowershell - 将 AD 中的所有计算机存储到数组中
【发布时间】:2013-06-07 00:29:08
【问题描述】:

所以目前我有一些代码可以通过 Powershell 中的 ADSI 锁定到特定的 OU,循环遍历并将它们存储到一个数组中。反过来,我循环遍历并运行测试连接。我有我的理由……

无论如何,是否有可能(仅使用内置 cmdlet,即没有 Quest 的东西)递归整个 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 2.0 还是 PowerShell 1.0 ?
  • 我会推荐 Quest 的 AD cmdlet,它们最适合我。

标签: powershell active-directory adsi


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在 V2 上使用 .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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多