Norrin Rad
Norrin Rad

Reputation: 991

powershell continue in loop till variable

Hi I'm trying to create a loop so that it continues to "do stuff" until a variable is hit.

what I'm trying to do is to loop through multiple disks and copy them, and then move to the next set of disks and copy those, hoever if there are no further sets then I would like to stop.

Example

if ($server -eq 1){
write-host "server 1"
}
if ($server -eq 2){
write-host "server 1"
write-host "server 2"
}
if ($server -eq 3){
write-host "server 1"
write-host "server 2"
write-host "server 3"
}
if ($server -eq 4){
write-host "server 1"
write-host "server 2"
write-host "server 3"
write-host "server 4"
}

server names would be substituted for disk names.

Is there a way to do if (server -eq 4) then it does server 1, server 2 and server 3.

The script is ending up with over 3000 lines, mostly repeated code.

Hope that makes sense

Thanks in advance :)

Upvotes: 0

Views: 53

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13237

Use a loop like you suggest?

$servers = 4 #update with number of servers

for ($i=1; $i -le $servers; $i++) {
    Write-Host "server $i"
}

Upvotes: 1

Related Questions