【问题标题】:Bug when submitting properties to Get-ADUser? Saving to CSV File (Powershell)向 Get-ADUser 提交属性时出现错误?保存到 CSV 文件 (Powershell)
【发布时间】:2021-02-25 18:22:17
【问题描述】:

这是一个非常简化的 Powershell 脚本版本,我想用它来为给定 OU 中的用户返回 ADUser 属性(以 CSV 格式),例如姓名、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”,用于显示我希望看到的每个用户的详细信息...

我已尽我所能找出问题所在。如果提交的文本是单个单词,例如“名称”,那么一切正常。如果我再添加任何内容,即使没有空格,问题也会再次出现。

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

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

【问题讨论】:

    标签: windows powershell active-directory


    【解决方案1】:

    我已尽我所能找出问题所在。如果提交的文本是单个单词,例如“名称”,那么一切正常。如果我再添加任何内容,即使没有空格,问题也会再次出现。

    你真的很亲密!

    Select-Object 接受属性名称或属性表达式的数组 - 而不是单个逗号分隔的字符串。

    当您将字符串 "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 ...
    

    【讨论】:

    • 太棒了!我做了一些快速测试,看起来一切都准备好了。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2021-06-16
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多