PowerShell:如何_exclusively_检索特定属性

Mat*_*ert 3 powershell active-directory

我怎样才能检索到一个特定的属性完全

我知道select-objectcmdlet 在这方面似乎有些过时:

PS C:\> Get-ADOrganizationalUnit -SearchBase 'OU=Houston,DC=contoso,DC=net' -Filter 'Name -like "SomeOU"' -Properties * | Select-Object Description,Streetaddress,State,postalcode | format-list
Run Code Online (Sandbox Code Playgroud)

一个优化的版本是:

PS C:\> Get-ADOrganizationalUnit -SearchBase 'OU=Houston,DC=contoso,DC=net' -Filter 'Name -like "SomeOU"' -Properties Description,Streetaddress,State,postalcode
Run Code Online (Sandbox Code Playgroud)

为什么-property开关不仅仅返回输入的属性?

Rea*_*ces 7

这是因为-propertiesswitch 不是格式化工具,它旨在作为一种接收比已包含的默认值更多的信息的方式。

来自Get-ADOrganizationalUnit文章。

指定要从服务器检索的输出对象的属性。使用此参数可检索未包含在默认设置中的属性。

如果要格式化输出,最好坚持使用该select-object语句。


Mat*_*sen 6

您需要同时使用两者,以指定要从 DC检索哪些属性,以及要选择并最终显示哪些属性。-Properties *是潜在的性能破坏者,因为 DSA 将需要返回具有值的每个属性,包括证书和您可能不需要的其他二进制值

在使用 AD cmdlet 的脚本中,我将使用splatting运算符 ( @) 并执行以下操作:

$ADSplat = @{
    "SearchBase" = 'OU=Houston,DC=contoso,DC=net'
    "Filter"     = 'Name -eq "SomeOU"'
    "Properties" = "Description","Streetaddress","State","postalcode"
}
$ADOU = Get-ADOrganizationalUnit @ADSplat | Select-Object $ADSplat["Properties"]
Run Code Online (Sandbox Code Playgroud)

如果您始终如一地执行此操作,您的脚本将变得非常容易更新/编辑,因为每个查询都以相同的可读 hastable 格式定义


如果我在 shell 和 mid-oneliner 来考虑我需要的属性,我会做类似的事情(Get-ADUser例如):

Get-ADUser username -Properties ($p = "manager","memberOf","cn") |select $p
Run Code Online (Sandbox Code Playgroud)