简体   繁体   English

如何使用Powershell从活动目录中获取独特的部门?

[英]How to get unique departments from active directory using Powershell?

I would like to get a list of unique departments from Active Directory using PowerShell. 我想使用PowerShell从Active Directory获取一个独特部门的列表。

Current code: 当前代码:

Import-Module activedirectory
get-aduser -filter * -property department |select department | sort-object property -unique

This returns a list titled "department" with no data. 这将返回一个标题为“department”的列表,没有数据。 How do I get a list of all Departments? 我如何获得所有部门的清单?

Your issue is solved in one of two ways 您的问题可通过以下两种方式之一解决

get-aduser -filter * -property department | select department | sort-object department -unique

or 要么

get-aduser -filter * -property department | select -ExpandProperty department | sort-object  -unique

In your example you have an object with the property department. 在您的示例中,您有一个属性部门的对象。 You then request that to be sorted on a property called property which does not exist. 然后,您请求在名为property上排序,该属性不存在。

You either use -ExpandProperty to convert the results to a string array or request sort-object to sort on the department property. 您可以使用-ExpandProperty将结果转换为字符串数组,也可以请求sort-object对department属性进行排序。

Couple of other options that will net similar results. 几个其他选项将净相似的结果。 Mileage will vary depending on your PowerShell Version. 里程将根据您的PowerShell版本而有所不同。

get-aduser -filter * -property department | select -ExpandProperty department -Unique
(get-aduser -filter * -property department).department | Sort-Object -Unique

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM