NorwegiaMan
NorwegiaMan

Reputation: 97

Merging 2 scripts

I've been tasked with compiling a list of all our security groups and grabbing specific information about these groups but I cannot for the life of me figure out how to get all the information onto one Csv.

This does a great job at getting all the bulk items I need from these groups.

Get-ADGroup -Filter * |

where {$_.GroupCategory -eq "security"} |
Where {$_.DistinguishedName -notlike "*OU=Builtin*"} |
Where {$_.DistinguishedName -notlike "*CN=Builtin*"} |
Where {$_.DistinguishedName -notlike "*OU=Microsoft Exchange Security Groups*"} |

Select DistinguishedName, GroupScope, Name, SamAccountName |

Export-csv -LiteralPath C:\Results\ListSecGroups.csv -NoTypeInformation

And this script is my attempt at getting all the information to compile together with a count of the group memberships.

$Groups = Get-ADGroup -Filter * |

where {$_.GroupCategory -eq "security"} |
Where {$_.DistinguishedName -notlike "*OU=Builtin*"} |
Where {$_.DistinguishedName -notlike "*CN=Builtin*"} |
Where {$_.DistinguishedName -notlike "*OU=Microsoft Exchange Security Groups*"}

ForEach ($Group in $Groups){

    (Get-ADGroup $Group.DistinguishedName -Properties *).member.count

    Select DistinguishedName, GroupScope, Name, SamAccountName |

    Export-csv -LiteralPath C:\Results\ListSecGroups.csv -NoTypeInformation -Append

    }

I will also be adding in another column for the date these groups have been edited, that way we can see what groups are truly inactive despite having members.

Upvotes: 0

Views: 47

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174845

For this you'll want to use a calculated property expression when calling Select-Object:

Get-ADGroup -Filter * |
  Where {$_.GroupCategory -eq "security"} |
  Where {$_.DistinguishedName -notlike "*OU=Builtin*"} |
  Where {$_.DistinguishedName -notlike "*CN=Builtin*"} |
  Where {$_.DistinguishedName -notlike "*OU=Microsoft Exchange Security Groups*"} |
  Select DistinguishedName, GroupScope, Name, SamAccountName, @{Name='MemberCount';Expression={(Get-ADGroup $_.DistinguishedName -Properties member).member.Count}} |
  Export-csv -LiteralPath C:\Results\ListSecGroups.csv -NoTypeInformation

This will create a new property named MemberCount for each input object, and populate it with the member count

Upvotes: 1

Related Questions