简体   繁体   English

Powershell 让 Groupmembers 进入 Out-GridView

[英]Powershell Get-Groupmembers into Out-GridView

I wanna use Out-GridView to display members of a selected AD group.我想使用 Out-GridView 来显示选定 AD 组的成员。 It would be nice if I could get all members (computers, other groups, users) but at least users is mandatory.如果我可以得到所有成员(计算机、其他组、用户),但至少用户是强制性的,那就太好了。 I have this code now:我现在有这个代码:

    Import-Module ActiveDirectory
$groups = Get-ADGroup -Filter * -Searchbase "OU=Groups,DC=domain,DC=int"|
    Select-Object @{n="Group"; e={$_.Name}}, DistinguishedName |Sort-Object "Group"|
    Out-GridView -Title "Select a group, then click OK"  -PassThru
$accounts = Foreach ($group in $groups) {Get-ADGroupMember -Identity $group.DistinguishedName -Recursive}

$report = Get-ADUser -Identity $account -Properties *|
    Select-Object name, SamAccountName, EmailAddress, EmployeeID, TelephoneNumber, Created, Department, City| 
    Out-GridView -Title "The members of the group"  -PassThru

At the moment I can search for the group , select it and then I do not get all the members.目前我可以搜索组,选择它,然后我没有得到所有成员。 just one, I think.只有一个,我想。 And also only a user cause it's Get-ADuser.而且也只有一个用户,因为它是 Get-ADuser。 Can anyone help me?谁能帮我?

Or maybe there is a similar powershell frontend somewhere in the internet?或者也许在互联网的某个地方有一个类似的 powershell 前端?

Get-ADGroupMember -Identity *group* | Out-GridView

This should get you all the members of the group.这应该让您获得该组的所有成员。 I guess you can filter it from there?我猜你可以从那里过滤它? :) :)

Since Get-ADGroupMember can return 3 different types of AD objects, you cannot blindly use Get-ADUser on each of the returned objects.由于Get-ADGroupMember可以返回 3 种不同类型的 AD 对象,因此您不能对每个返回的对象盲目使用Get-ADUser
What is more, not all of these different objects have the same properties you want shown in your grid view, so you need some method of capturing properties they have in common, while leaving others blank.此外,并非所有这些不同的对象都具有您希望在网格视图中显示的相同属性,因此您需要一些方法来捕获它们的共同属性,同时将其他属性留空。

Try:尝试:

Import-Module ActiveDirectory

$groups = Get-ADGroup -Filter * -Searchbase "OU=Groups,DC=domain,DC=int" |
          Select-Object @{Name = "Group"; Expression = {$_.Name}}, DistinguishedName | Sort-Object "Group"

# show the groups in a grid view and have the user select one item  
$selected = $groups | Out-GridView -Title "Select a group, then click OK" -PassThru

# if not cancelled
if ($selected) {
    # loop through the members of the selected group and capture the resulting objects in variable $result
    $result = foreach ($member in (Get-ADGroupMember -Identity $selected.DistinguishedName -Recursive)) {
        $account = switch ($member.objectClass) {
            'user' { 
                # Get-ADUser by default returns these properties:
                # DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
                Get-ADUser -Identity $member.DistinguishedName -Properties EmailAddress, EmployeeId, 
                                                                           OfficePhone, Created, Department, City
            }
            'group' {
                # Get-ADGroup by default returns these properties:
                # DistinguishedName, GroupCategory, GroupScope, Name, ObjectClass, ObjectGUID, SamAccountName, SID
                Get-ADGroup -Identity $member.DistinguishedName -Properties mail, Created |
                # rename the property 'mail' here
                Select-Object *, @{Name = 'EmailAddress'; Expression = {$_.mail}} -ExcludeProperty mail
            }
            'computer' {
                # Get-ADComputer by default returns these properties:
                # DistinguishedName, DNSHostName, Enabled, Name, ObjectClass, ObjectGUID, SamAccountName, SID,  UserPrincipalName
                Get-ADComputer -Identity $member.DistinguishedName -Properties Created
            }
        }
        # output an object with all properties you want in the grid view. Some will be empty though depending on the object type
        $account | Select-Object @{Name = 'Type'; Expression = {$member.objectClass}}, 
                                 Name, SamAccountName, EmailAddress, EmployeeId, OfficePhone, Created, Department, City
    }
    # display the results
    $result | Sort-Object Type, Name | Out-GridView -Title "The members of group '$($selected.Name)'"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM