Round-Robin a list for home drive creation

This topic contains 4 replies, has 3 voices, and was last updated by  Rob Simmers 8 months, 2 weeks ago.

  • Author
    Posts
  • #61620

    Goldy
    Participant

    Hi, i'm just looking for ideas on how to better utilize powershell to create a round-robin list to create home drives.

    I was thinking about creating an array and then writing the array object number to a file as a tracker and then just grabbing the next number in the list.

    Any thoughts?

  • #61632

    Rob Simmers
    Participant

    There are alot of ways to do things. If you don't need perfect balance, you can use Get-Random

    $shares = "Server1", "Server2", "Server3"
    $usingShare = $shares | Get-Random -Count 1
    
    "Using share {0}" -f $usingShare
    

    Otherwise, you could use Get-ChildItem to to get a count of shares per server and choose a server that has the least shares.

    • #61689

      Goldy
      Participant

      I was looking for perfect balance. I like the get-random idea, but I was more thinking sequencially 0-11 in the array.

      I was using this

      cls
      [int]$n = gc C:\Powershell\HD\hd_rr.txt -ea "silentlycontinue" | Select -last 1 
      write-host $n
      
      
      
      if ($n -eq 11) {
      
      new-item C:\Powershell\HD\hd_rr.txt -itemtype File -force 
      [int]$hd = 0
      } else {
      write-host "second"
      [int]$hd = $n + 1
      
      }
      
      write-host $hd
      
      ac -path C:\Powershell\HD\hd_rr.txt -value $hd
      
      $indexnumber = $hd
      
      

      My starter file has an entry of 0

  • #61699

    Fredrik Kacsmarck
    Participant

    Yes, if all you want to do is run it sequencially then all you need is the logging of the last number.

    If I use Rob's example of the shares variable.
    You could just call each specific share/whatever-data-you-store by it's index number.
    Eg.

    $shares[0]
    

    Will get you "Server1"
    If you replace the number with a variable.
    Eg.

    $shares[$n]
    

    You would then call the share/folder/whatever information that is stored with that index number.
    Replacing the content of $n wherever you are in the sequence.

  • #61707

    Rob Simmers
    Participant

    If you really want balance, the best approach is determining the number of folders that exist in the share on each server. Using an index still assumes that no one or nothing else will create folders in the share location. It will take longer to execute, but it's the best method to ensure balance.

You must be logged in to reply to this topic.