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
  • No matter how start writing this response, it quickly devolves into convolutions.

    I have a collection of objects returned from - $ALLServer = (get-qadcomputer -searchroot 'top OU for wintel servers' -includedproperties customAttribute8 -sizelimit 0 )

    I filter $ALLServers again into two collections of objects based on AD Site value (family of geographically similar AD sites lumped together.
    So, I threw this against the wall, and found that you can chunk out a collection of objects with PowerShell - two ways. (that know of)

    using Select ... wih 'first' and 'skip'
    $ALLServers | select -first 90 | to something
    $ALLServers | select -skip 90 -first 90 | do something
    $ALLServers | select -skip 180 -first 90 | do something
    ... until you have all 9 chunks

    For this example $Chunk_split value would be 100 if the $ALLServers.count was 900.

    Using 'ranges' ... i.e. 0..9
    $ALLServers[(($Chunk_split * 0) + 0)..($Chunk_split * 1)] | do something
    $ALLServers[(($Chunk_split * 1) + 1)..($Chunk_split * 2)] | do something
    $ALLServers[(($Chunk_split * 2) + 1)..($Chunk_split * 3)] | do something
    ... until you have processed all 9 chunks



    Either method above gives me a subset of the whole "foreach" chunk of objects which I pipe tot he following.

    | %{add-QADGroupMember -identity "CN of Group" -add-QADGroupMember -Member $_.dn -control @{OperationReason="MyReasonForDoingThis"} -proxy}

    so, I have a foreach, increment the value of $i++ - essentially sliding the window that I focus on - adding those servers to a group, then slide the window to the next chunk of servers in $ALLServers and add them to the list.

    I can do this with repetition of the same lines with incremented numbers ... I was hoping to automate.

    If that's still clear a mud, I can sanitize the script-in-progress and share so you can see for yourself how little I know of what I'm talking about.

    switch() time to google.
Reply
  • No matter how start writing this response, it quickly devolves into convolutions.

    I have a collection of objects returned from - $ALLServer = (get-qadcomputer -searchroot 'top OU for wintel servers' -includedproperties customAttribute8 -sizelimit 0 )

    I filter $ALLServers again into two collections of objects based on AD Site value (family of geographically similar AD sites lumped together.
    So, I threw this against the wall, and found that you can chunk out a collection of objects with PowerShell - two ways. (that know of)

    using Select ... wih 'first' and 'skip'
    $ALLServers | select -first 90 | to something
    $ALLServers | select -skip 90 -first 90 | do something
    $ALLServers | select -skip 180 -first 90 | do something
    ... until you have all 9 chunks

    For this example $Chunk_split value would be 100 if the $ALLServers.count was 900.

    Using 'ranges' ... i.e. 0..9
    $ALLServers[(($Chunk_split * 0) + 0)..($Chunk_split * 1)] | do something
    $ALLServers[(($Chunk_split * 1) + 1)..($Chunk_split * 2)] | do something
    $ALLServers[(($Chunk_split * 2) + 1)..($Chunk_split * 3)] | do something
    ... until you have processed all 9 chunks



    Either method above gives me a subset of the whole "foreach" chunk of objects which I pipe tot he following.

    | %{add-QADGroupMember -identity "CN of Group" -add-QADGroupMember -Member $_.dn -control @{OperationReason="MyReasonForDoingThis"} -proxy}

    so, I have a foreach, increment the value of $i++ - essentially sliding the window that I focus on - adding those servers to a group, then slide the window to the next chunk of servers in $ALLServers and add them to the list.

    I can do this with repetition of the same lines with incremented numbers ... I was hoping to automate.

    If that's still clear a mud, I can sanitize the script-in-progress and share so you can see for yourself how little I know of what I'm talking about.

    switch() time to google.
Children
No Data