简体   繁体   中英

How do i get Get-ADGroupMember $group | get-aduser -Properties enabled |select enabled to return a singe result/

I'm trying to get @{n='Active';e={(Get-ADGroupMember $group | get-aduser -Properties enabled |select enabled).enabled}} to read the samaccount name and return whether the user is enabled or disabled. However the code returns true and false for all members in the group. How do i get it to return result for just the user in 1st column in my results?

Below is my full script with the results I get.

$groups = "some","ad", "groups"

$results = foreach ($group in $groups) {
        Get-ADGroupMember  $group | select samaccountname, @{n='GroupName';e={$group}}, @{n='Description';e={(Get-ADGroup $group -Properties description).description}}, @{n='Active';e={(Get-ADGroupMember $group  | get-aduser -Properties enabled |select enabled).enabled}}
    }
    
    $results

Results: 结果

Thanks in advanced

I would recommend you to not use Select-Object in this case and use PSCustomObject instead, it will keep the code more readable (though this is personal opinion).

$groups = "some", "ad", "groups"

$results = foreach ($item in $groups) {
    $group = Get-ADGroup $item -Properties description
    foreach($member in Get-ADGroupMember $item) {
        if($member.ObjectClass -eq 'user') {
            $active = (Get-ADUser $member).Enabled
        } 
        else {
            $active = 'N/A'
        }

        [pscustomobject]@{
            GroupName      = $group.Name
            Description    = $group.Description
            samAccountName = $member.Name
            ObjectClass    = $member.ObjectClass
            Active         = $active
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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