簡體   English   中英

如何搜索Active Directory以查找來自同一域的兩個不同組織單位中存在的所有已啟用用戶?

[英]How do I search Active Directory to find all enabled users that exist in two different organizational units from the same domain?

我不太熟悉腳本編寫,但我希望定期審核Active Directory域中已啟用的用途。 我們將用戶分為多個組織單位,我想搜索所有已啟用的用戶,並將該信息導出到單個csv文件中,以供其他部門審核。

我想使用Powershell來做到這一點,但是我不喜歡這種方法。

現在,我正在使用以下代碼創建兩個文件,但是很難將信息細化為名字和姓氏,然后將來自不同單位的數據放入一個文件中。

任何幫助,將不勝感激。

Get-ADUser -Filter 'enabled -eq $true' -SearchBase "OU=corporate office,OU=company users,DC=company,DC=com" | export-csv -Path c:\files\corporate_users.csv

Get-ADUser -Filter 'enabled -eq $true' -SearchBase "OU=branch office,OU=company users,DC=company,DC=com" | export-csv -Path c:\files\branch_users.csv

好的,所以您要做的就是將第一個命令的結果作為數組存儲到變量中,然后將第二個命令的結果添加到該數組中,之后我們可以繼續過濾結果,然后導出為CSV文件。

$results = Get-ADUser -Filter 'enabled -eq $true' -SearchBase "OU=corporate office,OU=company users,DC=company,DC=com"
$results += Get-ADUser -Filter 'enabled -eq $true' -SearchBase "OU=branch office,OU=company users,DC=company,DC=com"

$results | select-object GivenName,SurName | export-csv -Path c:\files\branch_users.csv

請注意,如果您打算無論如何都要獲得所有啟用的用戶,則只需消除-SearchBase參數並僅使用過濾器即可運行Get-Aduser。 您可能還想嘗試運行Get-aduser SOMEUSERNAME -properties * | Get-Member Get-aduser SOMEUSERNAME -properties * | Get-Member ,它將顯示ADUSER對象上可用的(許多)屬性的名稱。

一個對象可以並且始終存在於Active Directory中的一個位置。 根據該斷言“否​​”,用戶不能同時存在於Active Directory域中的兩個不同的OU中。

因此,按照AD術語,用戶帳戶在OU中具有單值屬性,而在組中具有多值屬性。

您做的絕對正確。 我只是為您提供一個腳本,您可以根據需要使用它。 只需創建一個ps1文件並執行以下腳本。

我還在腳本中添加了注釋,以供您參考。

# First line is creating the CSV File and capturing only the Four Properties which I have passed in the Select part
Get-ADUser -Filter 'enabled -eq $true' -SearchBase "OU=corporate office,OU=company users,DC=company,DC=com" |Select Name,SamAccountName,DistinguishedName,Surname| export-csv -Path c:\files\corporate_users.csv

# Second line is Appending the data in the same csv file which the 1st line has been created with the same properties.
Get-ADUser -Filter 'enabled -eq $true' -SearchBase "OU=branch office,OU=company users,DC=company,DC=com" |Select Name,SamAccountName,DistinguishedName,Surname| export-csv -Path c:\files\branch_users.csv -Append

# You can segregate them using the DistinguisedName property which will tell that a user is part of which OU. 

注意:您可以根據需要在“選擇”中選擇“用戶”的所有屬性。

如果這個讓您滿意,請隨時接受答案,這也會對他人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM