仅从返回的Get-ADUser数组中获取字符串值

时间:2020-09-22 14:15:16

标签: powershell active-directory

我在Powershell中编写了一个小型Forms GUI,其中有一个组合框,其中包含来自AD的用户电子邮件。 现在,我在SelectedIndexChanged上收到一个事件,该事件要在标签中显示有关所选用户的更多信息。由于我有一个独特的电子邮件作为过滤器,因此Get-ADUser始终只有一个返回的对象。

我的问题是,当我使用select cn时,Get-ADUser返回一个字符串数组,即使使用Out-String,它也会返回具有返回数组格式的字符串。

当我得到返回的字符串时,我不想手动删除@{cn=}。那么,有没有更有效的方法来获得返回的字符串数组的值?

这是我尝试过的:

$label_name.Text = Get-ADUser -Properties mail -Filter {mail -like $comboBox_User.Text} | select cn | Out-String

返回的字符串如下:


cn
-
foo (returned name)
 

我期望的回报:

foo

1 个答案:

答案 0 :(得分:1)

您的查询缺少cn属性,因此请确保Get-ADUser中包含该属性,因为默认情况下不会返回该属性。展开CN属性。您不需要| Out-String

$label_name.Text = Get-ADUser -Properties mail,cn -Filter {mail -like $comboBox_User.Text} | Select-Object -expand cn

相关问题