Combine output from Get-ADGroupmember and Get-ADGroup from .csv file

Good afternoon experts!

I’m trying to get the following information from get-adgroupmember and get-adgroup groupname, groupdescription, groupinfo and groupmembers using the following script

$GNames = Import-CSV -Path D:\Powershell_temp\danea_info.csv| ForEach-Object {
Get-ADGroup -Filter “Name -like ‘$($_.name)’” -Properties * } | Select-Object Name

$results = foreach ($GName in $GNames) {
Get-ADGroupMember -ID $GName | select samaccountname, name, @{n=‘GroupName’;e={$GName}}, @{n=‘Description’;e={(Get-ADGroup $GName -Properties description).description}}
}

$results

Here is the error(s) I’m seeing:
Get-ADGroupMember : Cannot bind parameter ‘Identity’. Cannot create object of type “Microsoft.ActiveDirectory.Management.ADGroup”. The adapter cannot set the value of property “Name”.

Here is a snip from $GNames
Name

apabcd_Server_Test_Group
ClientVirusQuarantine
dgabcd3

Any and all input would be greatly appreciated

Norm

Got it to work sort of…
The following script works, but it only displays the ADGROUPS that contain members. My goal would be to list all groups contained in $GNames that is to say include the ADGROUPS with no group members.

$GNames = Import-CSV -Path D:\Powershell_temp\danea_info.csv| ForEach-Object {
Get-ADGroup -Filter “Name -like ‘$($_.name)’” -Properties * }

$results = foreach ($GName in $GNames) {
Get-ADGroupMember -ID $GName | select name, @{n=‘GroupName’;e={$GName}}, @{n=‘Description’;e={(Get-ADGroup $GName -Properties description).description}}
}

$results

Please format your code as code using the code tag button right above the edit windows of the post editor. Thanks.

I think you’d be better off using PSCustomObjects like this:

GNames = Import-CSV -Path D:\Powershell_temp\danea_info.csv |
    ForEach-Object {
        Get-ADGroup -Filter "Name -like '*($_.name)*’" -Properties *
}

$results = foreach ($GName in $GNames) {
$Members = Get-ADGroupMember -ID $GName
[PSCustomObject]@{
GroupName = $GName.Name
Description = $GName.Description
Members = $Members.Name
}
}

$results

… unteted, but should work actually … :wink:

Hello Olaf;
Thank you for your suggestion it gets me a whole lot closer to what I’m looking for! On problem remains when there is more than one member I’m getting the following display in the Members column:

System.Object[]

Here is actual output in $results for the row displaying System.Object[]:
{jsmith, jdoe}

Any thoughts as to why this is occurring?

Norm

Hi Olaf;
So the following script

$GNames = Import-CSV -Path D:\Powershell_temp\danea_info.csv| ForEach-Object { 
    Get-ADGroup -Filter "Name -like '*$($_.name)*'" } 
$results = foreach ($GName in $GNames) {
    Get-ADGroupMember -ID $GName | select name, @{n='GroupName';e={(Get-ADGroup $GName -Properties Name).Name}}, @{n='Description';e={(Get-ADGroup $GName -Properties description).description}}, @{n='Info';e={(Get-ADGroup $GName -Properties info).info}}
}

$results

Produces the following output
name GroupName Description Info


park dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
kim dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
chs dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
aroney dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
ngelpo dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
davis dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
amagna dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
krivan dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
arr dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
ivilla dgVPN-XXSVDI Two factor VPN - Access to XXS VDI
row BudgetCopyRO
clure BudgetCopyRO

As you can see there are two ADGroups containing multiple members, again your suggestion gets me very close to what I’m looking for, only issue is the output of group members.

Thanks again for your input
Norm

Try wrapping them in quotes,

Members = “$($Members.Name)”

Hello kvprasoon,
Well that worked thank you!! Now the next question is why, don’t understand why your suggestion worked. If you don’t have the time to explain that’s okay.

Thanks again
Norm

By the way how do I assign points?

As for this…

"$($Members.Name)"

… this is a way of expanding the content of the variable contents and / or grouped stings. This is a very common thing done in PS.

See this write-Up

Powershell String Expansion http://www.powershellish.com/blog/2014-12-09-strings-expansion

https://devblogs.microsoft.com/powershell/variable-expansion-in-strings-and-here-strings

As for this…

By the way how do I assign points?

… you don’t. It’s done by an underlying process of the site.

Thanks @postanote for the explanation.

@nlong12 I was doing a quick round up through the posts, hence missed to explain it.

no worries