Home > Mobile >  Merging 2 scripts
Merging 2 scripts

Time:05-12

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.

CodePudding user response:

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

  • Related