向 Get-ADUser 提交属性时出错?保存到 CSV 文件 (Powershell) [英] Bug when submitting properties to Get-ADUser? Saving to CSV File (Powershell)
问题描述
这是一个非常简化的 Powershell 脚本版本,我想用它为给定 OU 中的用户返回 ADUser 属性(以 CSV 格式),例如 name、givenName、Office 等.
Here is a very simplified version of Powershell script I'd like to use to return ADUser properties (in a CSV) such as name, givenName, Office etc for users in a given OU.
[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";对于我希望看到的每个用户的详细信息...
The script prompts the user to enter the comma separated properties into an InputBox, which is then saved as variable. On the saved CSV file, all I can see is "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection" for each of the users' details i was expecting to see...
我已尝试尽可能多地隔离问题.如果提交的文本是单个单词,例如name"然后一切正常.如果我再添加任何内容,即使没有空格,问题又会回来.
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.
此外,当我在 ISE 中运行脚本并选择不输出到 CSV 文件时,在控制台的每一行上,我将返回的是 OU 中每个用户的一组空大括号.
Also, when I run the script in ISE and choose not to output to a CSV file, on each line of the console all I"ll get back is a set of empty braces for each user in the OU.
解决这个问题对我来说意义重大.谢谢.
Getting this fixed, would mean a lot to me. Thanks.
推荐答案
我已尝试尽可能多地隔离问题.如果提交的文本是单个单词,例如name"然后一切正常.如果我再添加任何内容,即使没有空格,问题又会回来.
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
接受属性名称或属性表达式的数组 - 不是单个逗号分隔的字符串.
Select-Object
accepts an array of property names or property expressions - not a single comma-separated string.
当您将字符串 "name,email,manager"
传递给 Select-Object
时,它开始寻找文字名称为 name 的属性,email,manager
- 就像您尝试访问 $user.name,email,manager"
.
When you pass the string "name,email,manager"
to Select-Object
, it starts looking for a property with the literal name name,email,manager
- as if you'd tried to access $user."name,email,manager"
.
将字符串拆分为单独的属性名称,它将起作用:
Split the string into individual property names and it will work:
# ...
$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 ...
这篇关于向 Get-ADUser 提交属性时出错?保存到 CSV 文件 (Powershell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!