首页 > 解决方案 > 使用 Powershell 从 AD 组获取嵌套 CN 值

问题描述

我一直在尝试CN从 AD 组的属性中获取值managedBy,这是我获取组的代码

Get-ADGroup CACAL-ER-Marketing -Properties SamAccountName, managedBy, Name, Description, GroupCategory |
Select-Object SamAccountName, managedBy, Name, Description, GroupCategory

输出:

SamAccountName : CACAL-ER-Marketing
managedBy      : CN=Diane Dela Torre,OU=1-Mail Archive Needed,OU=User Archive,DC=hrbl,DC=net
Name           : CACAL-ER-Marketing
Description    : 
GroupCategory  : Security

我想得到的是'Diane Dela Torre'值,我尝试转换mananagedBy为Json,但它输出它不是一个cmdlet,比如

Get-ADGroup CACAL-ER-Marketing -Properties SamAccountName, managedBy, Name, Description, GroupCategory |
Select-Object SamAccountName, (managedBy | ConvertTo-Json).CN, Name, Description, GroupCategory

managedBy : The term 'managedBy' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:32
+ Select-Object SamAccountName, (managedBy | ConvertTo-Json).CN, Name,  ...
+                                ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (managedBy:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

标签: powershellactive-directory

解决方案


由于该managedBy属性是DistinguishedName用户的,而不是在其上使用正则表达式来获取该用户的显示名称,我将使用 Get-ADUser 来执行此操作。

Get-ADGroup CACAL-ER-Marketing -Properties SamAccountName, managedBy, Name, Description, GroupCategory |
Select-Object SamAccountName, 
              @{Name = 'ManagedBy'; Expression = { (Get-ADUser -Identity $_.managedBy -Properties DisplayName).DisplayName }},
              Name, Description, GroupCategory 

推荐阅读