如何搜索 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 来做这件事,但我不喜欢那种方法。

现在,我正在使用以下内容创建两个文件,但我很难将信息细化为名字和姓氏,然后将来自不同 ou 的数据放入一个文件中。

如有任何帮助,我们将不胜感激。

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。您可能还想尝试 运行ning 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. 

注意: 您可以根据需要在 Select 中选择用户的所有属性。

如果这个答案令您满意,请随时采纳,这对其他人也有帮助。