【问题标题】:Concatenating Items In A Collection连接集合中的项目
【发布时间】:2017-02-02 00:30:18
【问题描述】:

我有一个来自 ActiveDirectory 的 SearchResultCollection,名为 $results

$results = $directorySearcher.FindAll()

该集合中有两个 SearchResult 对象代表计算机。 我想要一个用逗号分隔的计算机名称的字符串:

$computerNames = $results | Select-Object { $_.Properties.name }
$computerNamesCommaSeparated = $computerNames -join ","

但是这给了我",",里面没有计算机名称。

我可以在$computerNames 中看到它具有以下内容:

$_.Properties.name
------------------
ComputerName1
ComputerName2

我怎样才能让它工作?

【问题讨论】:

  • $ComputerNames.Properties.name -join ","$computerNames = $results | ForEach-Object { $_.Properties.name } 然后加入。
  • 那行得通。谢谢。
  • @TessellatingHeckler 为什么不写一个答案而不是评论,以便我可以将其标记为已接受的答案?

标签: powershell active-directory


【解决方案1】:

试试这个:

$computerNamesCommaSeparated = $results.Properties.name -join ","

【讨论】:

  • 总是愚蠢的人不加解释就投反对票,这段代码有效!
  • 我认为您的代码将不起作用,因为 Properties 属性仅存在于 $results 中的每个元素上,而不存在于 $results 对象本身上。
  • 此代码有效,我已经测试过...验证您的确认之前...通过示例尝试此代码“(get-childitem -recurse).FullName”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 2013-07-13
  • 1970-01-01
相关资源
最近更新 更多