首页 > 解决方案 > 指定所有属性时,Get-ADUser 未返回所有可能的 AD 属性

问题描述

我遇到过使用
Get-ADUser -Properties *. 例如,
msDS-UserPasswordExpiryTimeComputed即使它存在,以下代码也不会列出该属性,我可以将其指定为
-Properties参数,让它返回,并可以处理它的值。

# Does not return msDS-UserPasswordExpiryTimeComputed
Get-ADUser username -Properties *

# This works to get the msDS-UserPasswordExpiryTimeComputed attribute returned
Get-ADUser username -Properties msDS-UserPasswordExpiryTimeComputed

# If I really want all properties and this one
# I have to specify it alongside *
Get-ADUser username -Properties *, msDS-UserPasswordExpiryTimeComputed

这不仅仅是从显示中省略属性的情况,我需要明确声明该msDS-UserPasswordExpiryTimeComputed属性,否则它在结果对象上根本不可用。

我已经知道在大多数情况下过滤Properties *并不是一个好主意,但我很好奇为什么没有枚举所有AD DS 属性,而这正是我要求 cmdlet 执行的操作。

这个问题正在询问,Get-ADUser但与Get-ADObjectcmdlet 的大多数其他行为一样,我认为这种行为扩展到大多数(如果不是全部)它们。

标签: powershellactive-directory

解决方案


以下代码应返回 AD 用户的所有属性(ObjectClass=user 的所有属性):

$properties = Get-ADObject -SearchBase (Get-ADRootDSE).SchemanamingContext -Filter {name -eq "User"} -Properties MayContain,SystemMayContain |
  Select-Object @{name="Properties";expression={$_.maycontain+$_.systemmaycontain}} |
  Select-Object -ExpandProperty Properties

Get-ADUser -Identity username -Properties $properties | fl $properties

首先,它检索所有用户属性并将其保存到一个数组中,然后将属性数组与 Get-ADUser 一起使用以检索单个用户的所有属性(在此示例中)。


推荐阅读