I have two files. One with a list of users. One with 3 servers. I need to
read each user and assign one server per user. How do I loop through the 3
servers? Like,
user1 - server1
user2 - server2
user3 - server3
user4 - server1
....
I have two files. One with a list of users. One with 3 servers. I need to
read each user and assign one server per user. How do I loop through the 3
servers? Like,
user1 - server1
user2 - server2
user3 - server3
user4 - server1
....
Im sure there is a more elegant way, but I think this is pretty clear cut
and easy to follow. Let me know if you have any questions.
---- CODE -----
$servers = get-content C:\servers.txt
$i = 0
foreach($user in get-content C:\users.txt)
{
if($i -lt ($servers.count-1)){Write-Host "$user - $($servers[$i])";$i++}
else{Write-Host "$user - $($servers[$i])";$i=0}
}
---- CODE -----
"Joe N" <JoeN@discussions.microsoft.com> wrote in message
news:AA21E52E-9325-4B31-BF6D-AA23B75FBD20@microsoft.com...
>I have two files. One with a list of users. One with 3 servers. I need
>to
> read each user and assign one server per user. How do I loop through the
> 3
> servers? Like,
>
> user1 - server1
> user2 - server2
> user3 - server3
> user4 - server1
> ...
On Jul 6, 10:42 pm, Joe N <J...@discussions.microsoft.com> wrote:
> I have two files. One with a list of users. One with 3 servers. I need to
> read each user and assign one server per user. How do I loop through the 3
> servers? Like,
>
> user1 - server1
> user2 - server2
> user3 - server3
> user4 - server1
> ...
Lots of ways of doing this, I would expect, depending on what you mean
by 'assign'. Here's another:
$users = get-content ".\users.txt"
$servers = get-content ".\servers.txt"
$users | % {$count = 0} {
if ($count -eq ($servers.length - 1)) {$count = 0}
write ("$_ > " + $servers[$count])
$count ++
}
-Hecks
Here is a fun way to assign the user to a random server. It doesn't even the
load, but its fun
$servers = get-content C:\PowershellAnalyzer\servers.txt
$rand = new-object system.random
get-content C:\PowershellAnalyzer\users.txt | %{$server =
$servers[$rand.Next(0,3)];write-Host "$_ - $server"}
"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message
news:ezAZvoBwHHA.5008@TK2MSFTNGP05.phx.gbl...
> Im sure there is a more elegant way, but I think this is pretty clear cut
> and easy to follow. Let me know if you have any questions.
>
> ---- CODE -----
> $servers = get-content C:\servers.txt
> $i = 0
> foreach($user in get-content C:\users.txt)
> {
> if($i -lt ($servers.count-1)){Write-Host "$user -
> $($servers[$i])";$i++}
> else{Write-Host "$user - $($servers[$i])";$i=0}
> }
> ---- CODE -----
>
> "Joe N" <JoeN@discussions.microsoft.com> wrote in message
> news:AA21E52E-9325-4B31-BF6D-AA23B75FBD20@microsoft.com...
>>I have two files. One with a list of users. One with 3 servers. I need
>>to
>> read each user and assign one server per user. How do I loop through the
>> 3
>> servers? Like,
>>
>> user1 - server1
>> user2 - server2
>> user3 - server3
>> user4 - server1
>> ...
>
Joe N wrote:
> I have two files. One with a list of users. One with 3 servers. I need to
> read each user and assign one server per user. How do I loop through the 3
> servers? Like,
>
> user1 - server1
> user2 - server2
> user3 - server3
> user4 - server1
> ...
>
--------------------------------------------------------------------------------------
Here is a way to do it.
It assumes that the both files does not have empty lines.
This way to use modulus operator. :-)
--------------------------------------------------------------------------------------
$servers=(gc servers.txt); gc users.txt | foreach {$i=0} { "$_ -
$($servers[$i % $servers.Length])"; $i++ }
--------------------------------------------------------------------------------------
hecks@hotmail.co.uk wrote:
> On Jul 6, 10:42 pm, Joe N <J...@discussions.microsoft.com> wrote:
>> I have two files. One with a list of users. One with 3 servers. I need to
>> read each user and assign one server per user. How do I loop through the 3
>> servers? Like,
>>
>> user1 - server1
>> user2 - server2
>> user3 - server3
>> user4 - server1
>> ...
>
> Lots of ways of doing this, I would expect, depending on what you mean
> by 'assign'. Here's another:
>
> $users = get-content ".\users.txt"
> $servers = get-content ".\servers.txt"
>
> $users | % {$count = 0} {
> if ($count -eq ($servers.length - 1)) {$count = 0}
> write ("$_ > " + $servers[$count])
> $count ++
> }
>
As $count is incremented after the if the -1 ist wrong :-(
should be:
$users | % {$count = 0} {
if ($count -eq ($servers.length)) {$count = 0}
write ("$_ > " + $servers[$count])
$count ++
}
--
Greetings
Matthias
"Joe N" <JoeN@discussions.microsoft.com> wrote in message
news:AA21E52E-9325-4B31-BF6D-AA23B75FBD20@microsoft.com...
>I have two files. One with a list of users. One with 3 servers. I need
>to
> read each user and assign one server per user. How do I loop through the
> 3
> servers? Like,
>
> user1 - server1
> user2 - server2
> user3 - server3
> user4 - server1
> ...
Or yet another way:
29> gc users.txt | %{$servers=gc servers.txt;$i=0} {"$_ -
$($servers[$i++])";$i%=3}
user1 - server1
user2 - server2
user3 - server3
user4 - server1
user5 - server2
user6 - server3
user7 - server1
user8 - server2
user9 - server3
user10 - server1
--
Keith
On Jul 7, 2:34 pm, Matthias Tacke <Matth...@Tacke.de> wrote:
> h...@hotmail.co.uk wrote:
> > On Jul 6, 10:42 pm, Joe N <J...@discussions.microsoft.com> wrote:
> >> I have two files. One with a list of users. One with 3 servers. I need to
> >> read each user and assign one server per user. How do I loop through the 3
> >> servers? Like,
>
> >> user1 - server1
> >> user2 - server2
> >> user3 - server3
> >> user4 - server1
> >> ...
>
> > Lots of ways of doing this, I would expect, depending on what you mean
> > by 'assign'. Here's another:
>
> > $users = get-content ".\users.txt"
> > $servers = get-content ".\servers.txt"
>
> > $users | % {$count = 0} {
> > if ($count -eq ($servers.length - 1)) {$count = 0}
> > write ("$_ > " + $servers[$count])
> > $count ++
> > }
>
> As $count is incremented after the if the -1 ist wrong :-(
> should be:
>
> $users | % {$count = 0} {
> if ($count -eq ($servers.length)) {$count = 0}
> write ("$_ > " + $servers[$count])
> $count ++
>
> }
>
> --
> Greetings
> Matthias
Oops, yes, you're right.
-Hecks
Thanks everyone. This one seemed to do the trick the best. My list of
servers would most likely be dynamic. I'm learning more stuff everyday.
| Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| for ... loop through file collection instead of for each ... loop | Ranah van Rijswijk | VB Script | 2 | 21 Feb 2010 |
| Fast copy method of sub array (=array range) possible? | Thomas Lebrecht | VB Script | 6 | 19 Mar 2009 |
| Stupid Array Tricks: Initializing an Array to a Certain Size | tojo2000 | PowerShell | 2 | 09 Sep 2008 |
| Re: loop through text and create array | Kiron | PowerShell | 1 | 20 May 2008 |
| how to assign values to array and how to create array via variable | Frank | PowerShell | 1 | 13 Mar 2007 |