如何在Powershell的输出中筛选特定细节

huangapple go评论58阅读模式
英文:

How to filter specific details in an output Powershell

问题

我想导出所有我的AD组以及其中的成员,并以干净的列表形式呈现。

我使用了基本命令:

Get-ADGroup -Filter * -Properties * | select CN, members

这个命令几乎给了我我想要的,但成员属性还包括了每个成员的组织单位(OU)和域控制器(DC),我想知道是否可以将输出从:

CN                   members 
--                   -------                  
Name of The Group    CN=Name of the User,OU=User Groups,OU=User,DC=DOMAIN,DC=com

更改为:

CN                   members
--                   -------
Name of the Group    User1, User2, User3

甚至保留CN=。是否可能过滤掉每个OU和DC?抱歉,我不太了解如何在这个网站上进行格式化。

英文:

So i want to export all my AD groups and the members inside of it in a clean list.

I used the basic command :

Get-ADGroup -Filter * -Properties * | select CN,members 

which almost gave me what i want but the members property also gives the OU and DC of every member, I was wondering if it is possible to change the output from

CN                   members 
--                   -------                  
Name of The Group    CN=Name of the User,OU=User Groups,OU=User,DC=DOMAIN,DC=com

to

CN                   members                                                                                                                                                                                                                               
--                   -------                                                                  
Name of the Group    User1, User2, User3

Or even keeping the CN=

Is it possible to filter out every OU and DC?
Sorry i don't really know how to format on this website

答案1

得分: 0

如果您需要成员的详细信息,可能需要额外调用 AD。

我建议仅检索您需要的属性,以便最小化流量。相反,

Get-ADGroup -Filter * -Properties *

我建议

Get-ADGroup -Filter * -Properties members

要迭代并获取成员的详细信息,例如 DisplayName。请注意,一些关键属性始终会被返回

Get-ADGroup -Filter * -Properties members | 
Select -ExpandProperty member |% {
  Get-ADUser -Id $_ -Properties "Displayname" 
}

如果您想将列表导出到 CSV 或 Excel,最好将其导出为普通列表,而不是 Group、ListOfUsers。最终脚本可能如下所示

$groups = Get-ADGroup -Filter * -Properties members
foreach ($group in $groups) {
    foreach($member in $members) {
        $user = Get-ADUser -Id $member -Properties DisplayName
        [PSCustomObject]@{
            Group = $group.Name
            UserName = $user.UserPrincipalName
            User = $userDisplayName
        }
    }
}

您将获得一个列表,类似于:

Group       userName              User
-----       --------              ----
GroupA      User1@domain.com      User 1
GroupA      User2@domain.com      User 2
GroupA      User3@domain.com      User 3
GroupB      User1@domain.com      User 1
GroupB      User4@domain.com      User 4
英文:

If you need details of the members, you may need additional calls to AD

I recommend to retrieve only those properties you need, so you can minimize the traffic. Instead

Get-ADGroup -Filter * -Properties *

I suggest

Get-ADGroup -Filter * -Properties members

To iterate and get the members details, for example DisplayName. Be aware that some key properties are always returned

Get-ADGroup -Filter * -Properties members | 
Select -ExpandProperty member |% {
  Get-ADUser -Id $_ -Properties "Displayname" 
}

I you want to export the list to a csv or excel, can be better export in a plain list instead Group, ListOfUsers. The final script can be something like

$groups = Get-ADGroup -Filter * -Properties members
foreach ($group in $groups) {
    foreach($member in $members) {
        $user = Get-ADUser -Id $member -Properties DisplayName
        [PSCustomObject]@{
            Group = $group.Name
            UserName = $user.UserPrincipalName
            User = $userDisplayName
        }
    }
}

you will a list like:

Group       userName              User
-----       --------              ----
GroupA      User1@domain.com      User 1
GroupA      User2@domain.com      User 2
GroupA      User3@domain.com      User 3
GroupB      User1@domain.com      User 1
GroupB      User4@domain.com      User 4

huangapple
  • 本文由 发表于 2023年6月26日 15:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定