简单的 Powershell:输出基本文本,而不是从 Get-ADOrganizationalUnit 格式化
这确实适用于几乎所有 PS 命令,但我正在运行:
Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase $strDomainDN -SearchScope OneLevel
并且输出如下所示:
$_.Name
------
OU1
OU2
OU3
...
我想要的只是不带标题的实际列表($_.Name 或“------”)。我知道这可能非常简单,但在尝试格式化字符串时也非常令人沮丧。任何帮助将不胜感激。
This really applies to almost any PS command but I'm running:
Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase $strDomainDN -SearchScope OneLevel
and the output looks like:
$_.Name
------
OU1
OU2
OU3
...
And all I want is the actual list with not heading ($_.Name or the "------"). I know this is probably amazingly simple but also very frustrating when trying to format strings. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有很多方法可以做到这一点,但一种方法是使用 foreach-object cmdlet 选择您想要的内容。假设您只需要 Name 列,您可以执行以下操作:
Out-String 强制它输出到字符串,而不是输出的每一行的字符串数组。
There are a bunch of ways to do this but one way is to pick off what you want with a foreach-object cmdlet. Assuming you want just the Name column you can do something like this:
The Out-String forces it to output to a string instead of an array of strings for each line of the output.
PowerShell 就是 Perl 和 .Net 重现时会发生的情况。
这不是一个答案,但安迪的答案如此有效的原因与您对这个新工具的不熟悉有关。 PowerShell 以对象“思考”。 PowerShell 对“你能给我一些组织单位吗?”这个问题的回答是少数 OU。这些对象具有方法和属性。 Perl 通过管道传输字符串,而 .NET 本身并不“通过管道”。 PowerShell 通过管道传输对象。
您收到的输出是 CLI 格式的对象属性表。您要求 PowerShell 返回名称,因此它返回包含 Name 属性的对象。为了以 CLI/文本方式方便地表示这些对象,PowerShell 通过调用其 toString() 方法生成一个带有标签、分隔符“-----”和数据行的表。
“Foreach”之所以具有魔力,是因为它可以让您控制格式,但您需要熟悉函数发出的对象这一点。 PowerShell 如此强大的原因在于直接操作“对象”本身,而不是它的字符串表示形式。
PowerShell is what would happen if Perl and .Net reproduced.
This isn't an answer, but the reason Andy's answer works so well has everything to do with your unfamiliarity with this new tool. PowerShell "thinks" in objects. PowerShell's answer to the question, "Can you get me some organizational units?" is a handful of OU's. Those objects have methods and properties. Perl pipes strings and .NET doesn't "pipe" per se. PowerShell pipes objects.
The output you received is a CLI-formatted table of object properties. You asked PowerShell to return names, so it returned objects containing the property of Name. To represent those objects conveniently in a CLI/text way, PowerShell generates a table with a label, a separator "-----", and rows of data by calling their toString() method.
"Foreach" does the magic because it lets you control the formatting, but you'll want to become comfortable with this thing of objects being emitted by functions. Direct manipulation of "the object" itself, rather than a string representation of it, is what makes PowerShell so powerful.
我遇到了同样的问题,但找到了一个更简单的解决方案。
在一般情况下,执行您运行的任何命令并查找可用属性,然后您可以按照与上面的“(...).Name”相同的方式隔离它们中的任何一个,例如“(...).Country” ' 或 '(...).State'。
I was having this same problem, but found a simpler solution.
and in the general case, take any command you run and find the available properties, and then you can isolate any of them in the same way as '(...).Name' above, e.g. '(...).Country' or '(...).State'.