繁体   English   中英

Powershell 让 Groupmembers 进入 Out-GridView

[英]Powershell Get-Groupmembers into Out-GridView

我想使用 Out-GridView 来显示选定 AD 组的成员。 如果我可以得到所有成员(计算机、其他组、用户),但至少用户是强制性的,那就太好了。 我现在有这个代码:

    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

目前我可以搜索组,选择它,然后我没有得到所有成员。 只有一个,我想。 而且也只有一个用户,因为它是 Get-ADuser。 谁能帮我?

或者也许在互联网的某个地方有一个类似的 powershell 前端?

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

这应该让您获得该组的所有成员。 我猜你可以从那里过滤它? :)

由于Get-ADGroupMember可以返回 3 种不同类型的 AD 对象,因此您不能对每个返回的对象盲目使用Get-ADUser
此外,并非所有这些不同的对象都具有您希望在网格视图中显示的相同属性,因此您需要一些方法来捕获它们的共同属性,同时将其他属性留空。

尝试:

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