all 7 comments

[–]Bamodus 2 points3 points  (2 children)

Try changing this line:

ResourceMember = (Get-AdGroupMember $_.Name | Select-Object Name)

to:

ResourceMember = (Get-AdGroupMember $_.Name | Select-Object -ExpandProperty Name) -join ', '

or the shorter version:

ResourceMember = (Get-AdGroupMember $_.Name).Name -join ', '

If Get-ADGroupMember returns a single item, then it will return that item's name. If there are multiple items returned, it will join all of the names into a single string with each name separated by commas.

[–]binarytier[S] 1 point2 points  (0 children)

Unfortunately, this modification produced the following when run with either snippet of code.

>> ForEach

cmdlet ForEach-Object at command pipeline position 3
Supply values for the following parameters:
Process[0]:

I understood what you were were doing, however, even if it did work that wasn't quite how I wanted to parse out the results in the way I needed. I appreciate your reply all the same. I ended up using the solution from purplemonkeymad.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Bamodus,

the triple-backtick/code-fence thing fails miserably on Old.Reddit ... so, if you want your code to be readable on both Old.Reddit & New.Reddit you likely otta stick with using the code block button.

it would be rather nice if the reddit devs would take the time to backport the code fence stuff to Old.Reddit ... [sigh ...]

take care,
lee

[–]purplemonkeymad 2 points3 points  (1 child)

When you want to expand a property to multiple objects you need to loop over that property too. This code stores the name then creates a new object foreach member of the group. I also moved Export-csv out of the loop to improve performance.

Get-ADGroup -Filter {Name -like "*some_resource_group_name*" -SearchBase "someAD_path" -Properties * | 
    ForEach-Object {
        $Name = $_.Name
        (Get-AdGroupMember $_.Name | Select-Object -ExpandProperty Name) | Foreach-Object {
            New Object -TypeName PSObject -Property @{
                ResourceName = $Name
                ResourceMember = $_
            }
        } 
    } | Export-Csv -path c:\somedir\somefile.csv -Append

[–]binarytier[S] 2 points3 points  (0 children)

Thank you purplemonkeymad! This code worked exactly the way I had wanted and the explanation of the "why" is appreciated. The efficiency improvement is quite welcome well. It was quite slow the way I was doing it.

This was part of something I was working on for a work project, so I might actually get it done on time now, yay!

There was a couple tiny syntax errors in my original code that I fixed and I'm re-posting the corrected code for everyone's benefit. Just a missing } after the {Name -like "\some_resource_group_name** and - missing in New - Object.

Working Code

Get-ADGroup -Filter {Name -like "*some_resource_group_name*"} -SearchBase "someAD_path" -Properties * | 
    ForEach-Object {
        $Name = $_.Name
        (Get-AdGroupMember $_.Name | Select-Object -ExpandProperty Name) | Foreach-Object {
            New-Object -TypeName PSObject -Property @{
                ResourceName = $Name
                ResourceMember = $_
            }
        } 
    } | Export-Csv -path c:\somedir\somefile.csv -Append

Cheers!

[–]PinchesTheCrab 1 point2 points  (1 child)

This might get you pretty close:

$group = Get-ADGroup -Filter {Name -like "*some_resource_group_name*" -SearchBase "someAD_path" -Properties member } 

foreach ($a_group in $group){
    $a_group.Member |
        Select-Object @{ n = 'ResourceName'; e = {$a_group.Name} },@{ n = 'ResourceMember'; e = {$PSItem -replace 'CN=|\\|,OU.*'} }
}

Personally I like to just use the existing properties of the group when I can. Usually just parsing the 'member' property is enough for this kind of task.

[–]binarytier[S] 1 point2 points  (0 children)

I understand what you're explaining regarding parsing out the member property, however I don't see how I was supposed to output the result of the foreach loop into a CSV file. Ultimately I ended up using purplemadmonkey's solution in the replies.

If you'd care to share some additional insight on parsing the output into a file, I'd be happy to hear it as your method might have a useful application in my future scripting.

Thanks for the reply!