【问题标题】:Extract list of users (millions) in Active Directory using Powershell使用 Powershell 提取 Active Directory 中的用户列表(百万)
【发布时间】:2019-04-08 07:54:05
【问题描述】:

我们有一个拥有 500 万用户的 Active Directory。尝试使用 powershell 脚本提取用户时,我们收到错误“Get-ADUser : This operation returned because the timeout period expired”。

已经尝试在网上搜索优化的脚本。下面是我们所拥有的。这适用于约 50 万用户。

Import-Module ActiveDirectory

$Users = Get-ADUser -SearchBase "CN=Users,DC=*****,DC=*****,DC=*****" -Server "*****" -ResultPageSize 1 -LDAPFilter "(&(objectCategory=User)(whenCreated>=20190101000000.0Z)(whenCreated<=20190131235959.0Z))" -Properties WhenCreated | Select-Object Name, WhenCreated

$Users | Export-Csv C:\Temp\January2019.csv -NoTypeInformation

【问题讨论】:

  • 你为什么将ResultPageSize设置为1??对于这么多用户,我宁愿将默认值 256 提高到 [Int32]::MaxValue (2147483647)。见Get-ADUser
  • 我已经尝试将它的值更改为 100/500/1000 并且也没有使用 ResultPageSize 但仍然出现错误。
  • 试试 adsisearcher 和 ResultPage Size 像 200 或更少到 100
  • 如果您将 Get-ADuser 直接通过管道传输到 csv 而不是将其存储在变量中,它是否有效?您也可以尝试增加 powershell 的可用内存:vmwareinsight.com/Tips/2016/6/5798868/…
  • 您的组织可能在Users OU 中有许多子OU。或许最好创建一个由这些子 OU 组成的数组并一次循环遍历它们,并在执行过程中添加到 CSV 文件中。

标签: powershell active-directory


【解决方案1】:

Get-ADUser 和 PowerShell 为您提供的所有其他 cmdlet 都很方便,但在性能方面却很糟糕。

最好使用 .NET 的 DirectorySearcher,PowerShell 有一个简写形式:[ADSISearcher]。这是更多的代码,是的,但它快得多。这是一个应该做你想做的事的例子(确保改变你的 OU 和服务器的前两行):

$server = "****"
$ou = "CN=Users,DC=*****,DC=*****,DC=*****"

$searcher = [ADSISearcher]"(&(objectCategory=User)(whenCreated>=20190101000000.0Z)(whenCreated<=20190131235959.0Z))"
$searcher.PropertiesToLoad.Add("whenCreated") #We only want the whenCreated attribute
$searhcer.PageSize = 200 #Get the users in pages of 200
$searcher.SearchRoot = [ADSI]"LDAP://$server/$ou"

$ADObjects = @()
foreach($result in $searcher.FindAll()) {
    #The SearchResultCollection doesn't output in PowerShell very well, so here we create
    #a PSObject for each results with the properties that we can export later

    [Array]$propertiesList = $result.Properties.PropertyNames
    $obj = New-Object PSObject
    foreach($property in $propertiesList) { 
        $obj | add-member -membertype noteproperty -name $property -value ([string]$result.Properties.Item($property))
    }
    $ADObjects += $obj
}

$ADObjects | Export-Csv C:\Temp\January2019.csv -NoTypeInformation

【讨论】:

    【解决方案2】:

    感谢您的所有帮助。我没有使用 Get-ADUser,而是使用 CSVDE(一种允许将数据导出到 CSV 文件的 LDIFDE 变体)毫无问题地提取用户。

    CSVDE -f D:\Temp\ADUseru.csv -d "CN=Users,DC=*****,DC=*****,DC=*****" -r "(& (objectClass=user)(objectCategory=person)(whenCreated>=20120101000000.0Z)(whenCreated

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      相关资源
      最近更新 更多