Author Posts

August 22, 2013 at 8:26 am

This script allows me to display a lst of security groups in an OU

`Get-ADGroup -Filter {GroupCategory -eq 'Security'} -searchbase 'My OU Here' 
`

This script allows me to use an input file containing groups to either display a list of groups,# uf members, and names of members or write that information to a file.

`$Grouplist = Get-Content c:\inputfiles\Grouplist3.txt
foreach($Group in $Grouplist) {
    $strgroup = Get-ADGroup -identity $Group -Properties * | select name,member
    $strGRP=($strgroup.MEMBER | Sort Name | foreach {($_.Split(','))[0].Substring(3)}) -Join ","
    $strGC=($strgroup.MEMBER).count
    $strgroup=($strgroup.name | foreach {($_.Split('='))[0]}) -Join "," 
    $strDONE="$strgroup,$strGC,$strGRP"
#    Add-Content c:\outputfiles\TestOut.csv -value $strdone }
write-host $strDone }`

What I'd like to do is combine these two commands so that I can list all groups in an OU, how many users are in each group, and show their usernames

I'm just not sure how to do that. Can I get some assistance?

August 22, 2013 at 8:31 am

Please use the CODE button in the toolbar to format code. Works a lot betterer.

Start by putting the groups you want into a variable:


$groups = Get-ADGroup -Filter {GroupCategory -eq 'Security'} -searchbase 'My OU Here'

Then enumerate:


foreach ($group in $groups) {
$membercount = ($group.member).count
}

That allows you to work with one group at a time.

You'd then construct a custom object, I expect.


$props = @{'Group'=$group.name;
'Members'=$membercount;
'Whatever'=$group.whatever}
$obj = New-Object -Type PSObject -Prop $props
Write $obj

You'll have to decide what you want to include in $props, which becomes part of your output. I'll warn you that working with object hierarchies can be tricky, and that's what you're asking to do. A group is a single entity; each of its members is also an entity. PowerShell doesn't provide awesome default formatting for entities that have sub-entities, so you'll need to decide how you want to use that output and do a bit of work to get there.

August 22, 2013 at 9:53 am

Hmm... I tried the Code button once before, and thought it looked terrible. Testing:

foreach ($item in $collection)
{
    if ($item.SomeBooleanProperty)
    {
        try
        {
            $item.SomeMethod()
        }
        catch
        {
            throw
        }
    }
}


foreach ($item in $collection)
{
if ($item.SomeBooleanProperty)
{
try
{
$item.SomeMethod()
}
catch
{
throw
}
}
}

August 22, 2013 at 9:53 am

Yeah, the Code tag is ugly as hell. All the indenting is removed.

August 22, 2013 at 10:01 am

Hmm. We maybe need to check what you're seeing.

When I press the CODE button, it generates an HTML CODE tag. That's handled the same as PRE, which you used in your goodlooking example.

Your second example used a back tick, which definitely does not work, is not intended, and we don't support.

Are you experiencing something other than I am when you click the CODE button on the toolbar?

August 22, 2013 at 10:02 am


This
is
a
code tag

This
  is
    a
     pre tag

August 22, 2013 at 10:02 am

Ah, I see. The CODE tag is being internally interpreted. That, I can put a stop to.

August 22, 2013 at 11:02 am

Just to take the forum software out of the equation, I tried putting the same text into PRE and CODE tags in a local HTML file. Even then, I'm not seeing how CODE is useful (since it stuck everything all on one line, instead of keeping the line breaks and spacing). See attached HTML file (renamed to .txt) and screenshot.