使用Powershell提取Active Directory中的用户列表(百万)

使用Powershell提取Active Directory中的用户列表(百万),powershell,active-directory,Powershell,Active Directory,我们有一个拥有500万用户的Active Directory。我们收到错误Get ADUser:此操作返回,因为尝试使用powershell脚本提取用户时超时时间已过期 已尝试在web上搜索优化脚本。下面是我们所拥有的。这适用于约500k用户 Import-Module ActiveDirectory $Users = Get-ADUser -SearchBase "CN=Users,DC=*****,DC=*****,DC=*****" -Server "*****" -ResultPage

我们有一个拥有500万用户的Active Directory。我们收到错误Get ADUser:此操作返回,因为尝试使用powershell脚本提取用户时超时时间已过期

已尝试在web上搜索优化脚本。下面是我们所拥有的。这适用于约500k用户

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
Get ADUser和PowerShell提供给您的所有其他cmdlet都很方便,但在性能方面却很糟糕

您最好使用.NET,PowerShell的缩写是:[AdiseArcher]。这是更多的代码,是的,但它要快得多。下面是一个示例,它应该做您想做的事情,确保更改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

谢谢你的帮助。我没有使用Get ADUser,而是能够使用CSVDE提取用户。CSVDE是一种LDIFDE变体,它允许将数据导出到CSV文件而没有任何问题


CSVDE-f D:\Temp\ADUseru.csv-D CN=Users,DC=****,DC=****,DC=****-r&objectClass=userobjectCategory=personwhenCreated>=20120101000000.0zwhencreated为什么要将ResultPageSize设置为1??对于这么多的用户,我宁愿将defaultvalue 256调到[Int32]::MaxValue 2147483647。请看,我已经尝试将其值更改为100/500/1000,也没有使用ResultPageSize,但仍然出现了错误。请改用AdiseArcher,ResultPage大小(如200或小于100)如果将Get ADuser直接传输到csv,而不是将其存储在变量中,是否有效?您还可以尝试增加powershell的可用内存:您的组织可能在用户OU中有许多子OU。也许最好创建这些子OU的数组,一次循环一个,并在运行时添加到CSV文件中。