This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Script Search - divide 1000 computers into 9 groups based on attribute value

I have what should be a very simple task on my plate that I can't seem to hit with a fork.

I have 9 groups and 2000 servers.  I need to split the servers between the groups ...

and as a maintenance workflow, when a new server comes online, after update of the OS value, move it into the least populated of the 9 groups.

I have a script started, that populates a native attribute on server objects with their AD site.  I divide the collected site names into two collections.  Each collection contains a couple thousand servers mapped to those sites.  I have 9  AD groups for each site collection..  I can split 2000 by 9, but here's the black hole in my logic, Nested Loops..

Do you have any scriptlets or script samples that demonstrate how to loop foreach group, add 1/9th of the 2000 servers into said group, then for the next group, grab the next 1/9th of the servers, etc ...  until done, amen.

fun times.

thanks in advance 

Parents
  • This is excellent, but I would suggest a modification to the sorting logic.

    Using the modulus operator, if you iterate the collection, you will only ever receive a value of 0, 1 , 2 , 3 , 4, 5, 6 , 7 or 8. Use these as your switch cases, and there is your sort.

    The pseudo-code would look like this:

    $Servers = Get-QADComputer -SearchRoot "OU=SomeOU,DC=MyDomain,DC=Local" | select distinguishedname

    Foreach ($ServersItem In $Servers)

    {

    # Increment the server counter

    $ServerNumber += 1

    # Figure out which group server 'x' should go into


    Switch ($ServerNumber % 9)

    { # Beginning of server group selection Switch

    0{ $ServerGroup="FirstServerGroup" }
    1{ $ServerGroup="SecondServerGroup" }
    2{ $ServerGroup="ThirdServerGroup" }
    3{ $ServerGroup="FourthServerGroup" }
    4{ $ServerGroup="FifthServerGroup" }
    5{ $ServerGroup="SixthServerGroup" }
    6{ $ServerGroup="SeventhServerGroup" }
    7{ $ServerGroup="EighthServerGroup" }
    8{ $ServerGroup="NinthServerGroup" }


    } # End of server group selection Switch

    Add-QADGroupMember -Identity $ServerGroup -Member $($ServersItem.distinguishedname)

    } # End of Servers loop
Reply
  • This is excellent, but I would suggest a modification to the sorting logic.

    Using the modulus operator, if you iterate the collection, you will only ever receive a value of 0, 1 , 2 , 3 , 4, 5, 6 , 7 or 8. Use these as your switch cases, and there is your sort.

    The pseudo-code would look like this:

    $Servers = Get-QADComputer -SearchRoot "OU=SomeOU,DC=MyDomain,DC=Local" | select distinguishedname

    Foreach ($ServersItem In $Servers)

    {

    # Increment the server counter

    $ServerNumber += 1

    # Figure out which group server 'x' should go into


    Switch ($ServerNumber % 9)

    { # Beginning of server group selection Switch

    0{ $ServerGroup="FirstServerGroup" }
    1{ $ServerGroup="SecondServerGroup" }
    2{ $ServerGroup="ThirdServerGroup" }
    3{ $ServerGroup="FourthServerGroup" }
    4{ $ServerGroup="FifthServerGroup" }
    5{ $ServerGroup="SixthServerGroup" }
    6{ $ServerGroup="SeventhServerGroup" }
    7{ $ServerGroup="EighthServerGroup" }
    8{ $ServerGroup="NinthServerGroup" }


    } # End of server group selection Switch

    Add-QADGroupMember -Identity $ServerGroup -Member $($ServersItem.distinguishedname)

    } # End of Servers loop
Children
No Data