mashing scripts

This topic contains 8 replies, has 3 voices, and was last updated by  Dave Wyatt 4 years, 3 months ago.

  • Author
    Posts
  • #9697

    notarat
    Participant

    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?

  • #9698

    Don Jones
    Keymaster

    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.

  • #9699

    notarat
    Participant

    Thanks Don

  • #9701

    Dave Wyatt
    Moderator

    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
    }
    }
    }

  • #9702

    Dave Wyatt
    Moderator

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

  • #9703

    Don Jones
    Keymaster

    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?

  • #9704

    Don Jones
    Keymaster


    This
    is
    a
    code tag

    This
      is
        a
         pre tag
    
  • #9705

    Don Jones
    Keymaster

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

  • #9711

    Dave Wyatt
    Moderator

    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.

You must be logged in to reply to this topic.