locked
Trying to select every 4th computer using Get-ADComputer RRS feed

  • Question

  • is there any easy way to select every 4th computer from an OU and add those devices to a security group?
    Thursday, August 3, 2017 5:55 PM

Answers

  • for($i = 0;$i -lt $computers.Count;$i+=4){
          $computers[$i]
    }


    \_(ツ)_/


    • Edited by jrv Thursday, August 3, 2017 6:05 PM
    • Marked as answer by escott0699 Thursday, August 3, 2017 6:25 PM
    Thursday, August 3, 2017 6:04 PM
  • $group = for($i = 0;$i -lt $computers.Count;$i+=4){
          $computers[$i]
    }
    for($i = 0;$i -lt $computers.Count;$i+=4){
          Add-AdGroupMember groupname -member $computers[$i]
    }



    \_(ツ)_/



    • Edited by jrv Thursday, August 3, 2017 6:25 PM
    • Marked as answer by escott0699 Thursday, August 3, 2017 6:26 PM
    Thursday, August 3, 2017 6:24 PM

All replies

  • for($i = 0;$i -lt $computers.Count;$i+=4){
          $computers[$i]
    }


    \_(ツ)_/


    • Edited by jrv Thursday, August 3, 2017 6:05 PM
    • Marked as answer by escott0699 Thursday, August 3, 2017 6:25 PM
    Thursday, August 3, 2017 6:04 PM
  • Thanks that seems to work well, my only issue is if i try to add those computers to a variable i end up with only a single device, where if i just read the output i am seeing each computer..
    Thursday, August 3, 2017 6:22 PM
  • $group = for($i = 0;$i -lt $computers.Count;$i+=4){
          $computers[$i]
    }
    for($i = 0;$i -lt $computers.Count;$i+=4){
          Add-AdGroupMember groupname -member $computers[$i]
    }



    \_(ツ)_/



    • Edited by jrv Thursday, August 3, 2017 6:25 PM
    • Marked as answer by escott0699 Thursday, August 3, 2017 6:26 PM
    Thursday, August 3, 2017 6:24 PM
  • Here is how to distribute objects in a collection over n groups.

    # group a collection over "n" groups
    $q = [System.Collections.Queue]::new()
    1..60 | %{$q.Enqueue($_)}
    $n = 0
    $groups = @(@(),@(),@(),@())
    while ($q.Count) {
    	$groups[$n]+=$q.Dequeue()
    	$n = (($n += 1) % 4)
    }
    

    This gives equal distribution over all groups.


    \_(ツ)_/

    Thursday, August 3, 2017 6:46 PM