如何從多個AD中提取用戶

如何從多個AD中提取用戶

我需要提取包含所有 AD 的檔案。我正在嘗試使用 Get-ADuser 來做到這一點。但是,由於 -Searchbase 不接受多個來源,我做了這樣的事情:

    'DC=AD1,DC=net','DC=1D2,DC=net','DC=AD3,DC=net','DC=AD4,DC=net' |
foreach-object{
get-aduser -SearchBase $_ -Filter { ( Enabled -eq $True ) -and ( (sn -ne 'Empty') -or (givenName  -ne 'empty')) -and ( (telephoneNumber -ne 'empty') -or (mobile -ne 'empty'))} -Properties * |Select sn,givenName,title,department,company,telephoneNumber,mobile,mail,employeeType,physicalDeliveryOfficeName,extensionAttribute15 | Export-CSV "c:\temp\Liste_collaborateurs.csv" -Encoding UTF8 -Delimiter ";" -NoTypeInformation
}

但是當我運行它時,我收到此錯誤 3 次:

get-aduser :提供的 DistinguishedName 必須屬於下列分割區之一: 'DC=bva,DC=net , CN=Configuration,DC=bva,DC=net , CN=Schema,CN=Configuration,DC= bva,DC = net ,DC = DomainDnsZones,DC = bva,DC = net,DC = ForestDnsZones,DC = bva,DC = net'。在 C:\Users\adm.wfd\Desktop\getaduser.ps1:3 char:1 + get-aduser -SearchBase $_ -Filter { ( Enabled -eq $True ) -and ( (sn -ne 'Empty' .. . + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException + ExcellentQualifiedErrorId : ActiveDirectoryCmdlet: System.ArgumentException、Microsoft.ActiveDirectory.Management.Comm 與s.GetADUser

我猜這 3 次是針對我的伺服器未連接到的每個 AD 的。我無法在每個 DC 上運行我的命令...

如何在單一伺服器上取得所有使用者的 csv?

感謝您的回覆

答案1

您確定使用正確的網域控制器嗎?或者拼字和順序是否合適?我在自己的環境中使用多個網域控制器測試了您的腳本,並且它有效。只有當我輸入錯誤訊息時才會出現您的錯誤訊息。

當不在循環中執行時,該命令是否適用於各個網域控制器?

如果直接在網域控制站上啟動腳本會怎樣?

我自己沒有測試過。

像那樣。

$Servers = 'server1','server2'            
ForEach ($Server in $Servers) {            
    Invoke-Command -ComputerName $Server -ScriptBlock {            
        get-aduser -Filter { ( Enabled -eq $True ) -and ( (sn -ne 'Empty') -or (givenName  -ne 'empty')) -and ( (telephoneNumber -ne 'empty') -or (mobile -ne 'empty'))} -Properties * |Select sn,givenName,title,department,company,telephoneNumber,mobile,mail,employeeType,physicalDeliveryOfficeName,extensionAttribute15 | Export-CSV "c:\temp\Liste_collaborateurs.csv" -Encoding UTF8 -Delimiter ";" -NoTypeInformation         
    }            
} 

相關內容