向 Get-ADUser 提交属性时出现错误?保存到 CSV 文件 (Powershell)

Bug when submitting properties to Get-ADUser? Saving to CSV File (Powershell)

这是一个非常简化的 Powershell 脚本版本,我想将其用于 return ADUser 属性(在 CSV 中),例如给定 OU 中用户的名称、givenName、Office 等。

[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$title = 'Get-ADUser Properties'
$msg   = 'Enter desired User properties, each seperated by a comma:'
$default = "name, office"
$propertiesSought = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title, $default)

get-aduser -filter * -properties * -searchbase $OU | select $propertiesSought | Export-Csv -Path "C:\Users\Administrator\Desktop\temp.csv"

脚本提示用户将逗号分隔的属性输入到 InputBox 中,然后将其保存为变量。在保存的 CSV 文件中,对于我希望看到的每个用户的详细信息,我只能看到“Microsoft.ActiveDirectory.Management.ADPropertyValueCollection”...

我已尽力找出问题所在。如果提交的文本是单个单词,例如“name”,那么一切都可以。如果我再添加任何东西,即使没有空格,问题又来了。

此外,当我 运行 ISE 中的脚本并选择不输出到 CSV 文件时,在控制台的每一行上,我得到的只是一组空括号,用于OU.

解决这个问题对我来说意义重大。谢谢

I've tried to isolate the problem as much as I can. If the text submitted is a single word such as "name" then everything is OK. If I add anything more, even without spaces, the problem comes back.

你真的很接近!

Select-Object 接受 array of 属性 names or 属性 expressions - not a single comma-separated string.

当您将字符串 "name,email,manager" 传递给 Select-Object 时,它开始寻找字面名称 name,email,manager 的 属性 - 就好像您尝试访问$user."name,email,manager".

将字符串拆分为单独的 属性 个名称,它将起作用:

# ...
$propertiesSought = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title, $default)

# Split input string
$propertiesSought = $propertiesSought -split ',\s*'

Get-ADUser -Filter * -Properties $propertiesSought -searchbase $OU |Select $propertiesSought |Export-Csv ...