I want to list the active directory name starting with prefix and its members.
Get-ADGroup
gives me list of group names I wanted. for the Get-ADGroupMember
command output I want to include group name, then list of members.
tried this code, but its giving me the error $_.Name
. How do we include group name part of Get-ADGroupMember
output?
Get-ADGroup -filter { name -like 'Test_*' -and GroupCategory -eq 'Security' } |
select-object -first 2 |ForEach-Object {
Get-ADGroupMember -Identity $_.Name | select $_.name,Name
}
Thanks
CodePudding user response:
select $_.name,Name
is a syntactical error, you need to use a calculated property to define the new property name and value:
$calculatedProps = @(
@{
Name = 'GroupName'
Expression = { $groupName }
}
@{
Name = 'Members'
Expression = { $_.Name }
}
)
Get-ADGroup -Filter "name -like 'Test_*' -and GroupCategory -eq 'Security'" |
Select-Object -First 2 | ForEach-Object {
$groupName = $_.Name
Get-ADGroupMember -Identity $_.DistinguishedName |
Select-Object $calculatedProps
}
I would also recommend you to stop using script blocks on the -Filter
parameter of the AD Module cmdlets to avoid future problems. Here is one example of the issues it might bring.