简单的 Powershell:输出基本文本,而不是从 Get-ADOrganizationalUnit 格式化

发布于 2024-11-08 03:31:22 字数 311 浏览 1 评论 0原文

这确实适用于几乎所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有你 2024-11-15 03:31:22

有很多方法可以做到这一点,但一种方法是使用 foreach-object cmdlet 选择您想要的内容。假设您只需要 Name 列,您可以执行以下操作:

13 >  dir | select name | foreach-object {$_.Name} | out-string
Contacts
Desktop
Documents
Downloads
Favorites
Links
Lync Recordings
Music
NetApp
Pictures
Podcasts
PowerShellASP
Saved Games
Searches
Tracing
Videos
Virtual Machines

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:

13 >  dir | select name | foreach-object {$_.Name} | out-string
Contacts
Desktop
Documents
Downloads
Favorites
Links
Lync Recordings
Music
NetApp
Pictures
Podcasts
PowerShellASP
Saved Games
Searches
Tracing
Videos
Virtual Machines

The Out-String forces it to output to a string instead of an array of strings for each line of the output.

不喜欢何必死缠烂打 2024-11-15 03:31:22

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.

何以笙箫默 2024-11-15 03:31:22

我遇到了同样的问题,但找到了一个更简单的解决方案。

(Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchScope OneLevel).Name

在一般情况下,执行您运行的任何命令并查找可用属性,然后您可以按照与上面的“(...).Name”相同的方式隔离它们中的任何一个,例如“(...).Country” ' 或 '(...).State'。

PS C:\temp\checkdirs> Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchScope OneLevel | gm


   TypeName: Microsoft.ActiveDirectory.Management.ADOrganizationalUnit

Name                     MemberType            Definition
----                     ----------            ----------
Contains                 Method                bool Contains(string propertyName)
Equals                   Method                bool Equals(System.Object obj)
GetEnumerator            Method                System.Collections.IDictionaryEnumerator GetEnumerator()
GetHashCode              Method                int GetHashCode()
GetType                  Method                type GetType()
ToString                 Method                string ToString()
Item                     ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(s...
City                     Property              System.String City {get;set;}
Country                  Property              System.String Country {get;set;}
DistinguishedName        Property              System.String DistinguishedName {get;set;}
LinkedGroupPolicyObjects Property              Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Linked...
ManagedBy                Property              System.String ManagedBy {get;set;}
Name                     Property              System.String Name {get;}
ObjectClass              Property              System.String ObjectClass {get;set;}
ObjectGUID               Property              System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=ne...
PostalCode               Property              System.String PostalCode {get;set;}
State                    Property              System.String State {get;set;}
StreetAddress            Property              System.String StreetAddress {get;set;}

I was having this same problem, but found a simpler solution.

(Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchScope OneLevel).Name

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'.

PS C:\temp\checkdirs> Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchScope OneLevel | gm


   TypeName: Microsoft.ActiveDirectory.Management.ADOrganizationalUnit

Name                     MemberType            Definition
----                     ----------            ----------
Contains                 Method                bool Contains(string propertyName)
Equals                   Method                bool Equals(System.Object obj)
GetEnumerator            Method                System.Collections.IDictionaryEnumerator GetEnumerator()
GetHashCode              Method                int GetHashCode()
GetType                  Method                type GetType()
ToString                 Method                string ToString()
Item                     ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(s...
City                     Property              System.String City {get;set;}
Country                  Property              System.String Country {get;set;}
DistinguishedName        Property              System.String DistinguishedName {get;set;}
LinkedGroupPolicyObjects Property              Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Linked...
ManagedBy                Property              System.String ManagedBy {get;set;}
Name                     Property              System.String Name {get;}
ObjectClass              Property              System.String ObjectClass {get;set;}
ObjectGUID               Property              System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=ne...
PostalCode               Property              System.String PostalCode {get;set;}
State                    Property              System.String State {get;set;}
StreetAddress            Property              System.String StreetAddress {get;set;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文