Help with Loop

I am trying to write a script that will ask me if I want to create a NIC team.  I'm trying to add some logic to it, so the only way to continue is to either answer "n" to the question or meet the condition.  The below kinda works, but if I meet the condition it will create the team but keeps asking me if I want to try again.

$askTeam = Read-Host "I can configure the NIC team for you.  Plug a cable into the two nics you want to team before continuing.`
Do you want me to create the NIC Team for you (Y/N)"
if ($askTeam -eq "Y"){
Do {
$Adapters = Get-NetAdapter | ?{$_.Status -eq "UP"}
$NetAdapter = $Adapters.Name
if ($NetAdapter.count -eq 2){
New-NetLbfoTeam -Name "Team #1" -TeamMembers ($NetAdapter[0],$NetAdapter[1]) -TeamingMode SwitchIndependent -LoadBalancingAlgorithm TransportPorts -Confirm:$false | Out-Null
Set-NetLbfoTeamMember -Name $NetAdapter[0] -AdministrativeMode Standby | Out-Null
}else{ $response = Read-Host "You must have exactly two NICs plugged in for me to successfully configure the NIC Team. Try again (Y/N)"}
}
While ($response -eq "y")
}
Thanks for your help

February 12th, 2015 8:59pm

The problem is the $response variable is only updated when your NetAdaptor count is not two.  You can actually put the Read-Host statement in the While clause in order for it to be always caught.  Just change the message to fit the condition.
Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 9:06pm

I did as you said and it did not work.  I don't want to answer the question after the condition is met.  When the condition is met, I want it to create the team and end the script.
February 12th, 2015 9:32pm

So set $response to something other than 'y'.
Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 9:41pm

$msg=@'
  I can configure the NIC team for you.  
  Plug a cable into the two nics you want to team before continuing. 
  Do you want me to create the NIC Team for you (Y/N)
'@
while('Y' -eq Read-Host $msg){
        $Adapters = Get-NetAdapter | ?{$_.Status -eq "UP"}
        if($NetAdapter.count -eq 2){
            New-NetLbfoTeam -Name "Team #1" -TeamMembers ($NetAdapter[0],$NetAdapter[1]) -TeamingMode SwitchIndependent -LoadBalancingAlgorithm TransportPorts -Confirm:$false | Out-Null
            Set-NetLbfoTeamMember -Name $NetAdapter[0] -AdministrativeMode Standby | Out-Null
        }else{
           Write-Host 'Not enough adapters' -Fore green
}

February 12th, 2015 9:51pm

Thanks jrv, still not working though.  To test I start with 3 nics plugged in, I get a message saying, I need to have only two plugged in, do I want to continue?... I unplug one nic, say "y" and it then creates the team for me, but it then tells me again that it can configure the nic team for me... I don't want it to ask me again if I want to create it after it's created.  I hope I am explaining myself right.  Thanks for your help guys
Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 10:07pm

I recommend learningPowerSHlle before trying to use it.

if(3 -ge 2){ ok }

February 12th, 2015 10:09pm

I did as you said and it did not work.  I don't want to answer the question after the condition is met.  When the condition is met, I want it to create the team and end the script.

What I said was a general idea and not a complete solution.  You'll need to figure out the detail of the logic.

Seems like jrv did the work for you.  I'd suggest you to go for some programming training which will help you a lot in the long run.

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 10:13pm

I thought I was in a Microsoft Forum asking for help not being told whether I need PowerShell classes or programming classes.  I wasn't rude or a jerk to anybody, I was simply trying to state my questions and trying to get the result I was looking for not the one you want to give me.

jrv did some work for me, and I appreciate it and it does look better than my script,  but if you look at his code it's also wrong (read-host $msg) not read-host $msg and you need to define the $NetAdapter alias which he did not do above.  Maybe I do know a couple things about PowerShell after all.

I tried your suggestions and it created the team but then asks again if I want to create the team and that is what I do not want. 

Why do you guys come over here to help people out if you are going to act like that?


February 12th, 2015 10:21pm

I thought I was in a Microsoft Forum asking for help not being told whether I need PowerShell classes or programming classes.  I wasn't rude or a jerk to anybody, I was simply trying to state my questions and trying to get the result I was looking for not the one you want to give me.

jrv did some work for me, and I appreciate it and it does look better than my script,  but if you look at his code it's also wrong (read-host $msg) not read-host $msg and you need to define the $NetAdapter alias which he did not do above.  Maybe I do know a couple things about PowerShell after all.

I tried your suggestions and it created the team but then asks again if I want to create the team and that is what I do not want. 

Why do you guys come over here to help people out if you are going to act like that?


This is a community sponsored and volunteer staffed forum. It is not MS support and has no guarantee or service agreement.

I showed you the way but I was not intending to rewrite your whole script, test it and deploy it.  That is your job. You said you had a problem with Read-Host management. "  I'm trying to add some logic to it, so the only way to continue is to either answer "n" to the question or meet the condition"

The loop will terminate if any key besides 'Y' is clicked.  The adapter bits, as you posted them, are incorrect as you need to enumerate the collection or you need to index into it.  No need for an assignment.

Here is an example:

       if($Adapter.count -ge 2){
            New-NetLbfoTeam -Name "Team #1" -TeamMembers ($Adapter[0],$Adapter[1]) -TeamingMode SwitchIndependent -LoadBalancingAlgorithm TransportPorts -Confirm:$false | Out-Null
            Set-NetLbfoTeamMember -Name $Adapter[0] -AdministrativeMode Standby | Out-Null

Wouldn't it be easier to just use the team configuration utility.  It does all of this and more.  This is one place where scripting is just plan silly.

If someone gives you a new car do you go into your garage and start building a car because you think it would be better?

As a scripting exercise it is useful but you need to do more homework.  The issues and solutions are part of "Scripting 101".  DOn't waste time with guesswork.

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 10:45pm

I can appreciate your last answer a lot better than telling me to go learn code.  I understand it's not MS support but doesn't have to be rude.  I didn't ask you to write it for me, as you can see in the first post, I wrote the script which probably just needs a minor tweak to make it work.  That's what I was looking for.  But whatever squash that.

If I do $adapter.count -ge 2 it will create the team even if I have 3 or more adapters, which I don't want.  I only want a nic team with 2 adapters.  I will keep trying.  Thanks for your input

February 12th, 2015 11:00pm

If I do $adapter.count -ge 2 it will create the team even if I have 3 or more adapters, which I don't want.  I only want a nic team with 2 adapters.  I will keep trying.  Thanks for your input

That is exactly what the original code said and you complained that it was wrong. That is why we suggest you do some studying and learn the basics of programming. We cannot run around in circles trying to fix you perception of how programs and computers work. It is your responsibility to learn this if you want to be a technician.

That is not rude it is just the facts of life.  Would you go to a brain surgeon that was teaching himself on the Internet in forums and chat rooms?

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 11:06pm

Dude, learn to read, my original post says -eq 2 not -ge.  I will do my best to try and be a great technician, so I can make you proud.  Get off your high horse. 

If what I end up with is based on any of the answers/suggestions I will come back and mark that post as the answer.

February 12th, 2015 11:47pm

$ask = Read-Host 'Set up teaming (y/n)?'

If ($ask -eq 'y') {

    Do {

        If ((Get-NetAdapter | Where { $_.Status -eq 'Up' }).Count -eq 2) {

            Write-Output 'Set up the team here'
            $ask = 'EXIT'

        } Else {

            $ask = Read-Host 'There must be exactly 2 adapters. Keep going (y/n)?'

        }

    } Until ($ask -ne 'y')

}

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 11:50pm

Thank you Mike, you pointed me in the right direction.  Here is the end result

$askTeam = Read-Host "I can configure the NIC team for you.  Plug a cable into the two nics you want to team before continuing.`
Do you want me to create the NIC Team for you (Y/N)"
if ($askTeam -eq "Y"){
Do {
$Adapters = Get-NetAdapter | ?{$_.Status -eq "UP"}
$NetAdapter = $Adapters.Name
if ($NetAdapter.count -eq 2){
Write-Output "Configuring NIC Team..."
New-NetLbfoTeam -Name "Team #1" -TeamMembers ($NetAdapter[0],$NetAdapter[1]) -TeamingMode SwitchIndependent -LoadBalancingAlgorithm TransportPorts -Confirm:$false | Out-Null
Set-NetLbfoTeamMember -Name $NetAdapter[0] -AdministrativeMode Standby | Out-Null
$askTeam = 'Continue'
}else{$askTeam = Read-Host "You must have exactly two NICs plugged in for me to successfully configure the NIC Team. Try again (Y/N)"}
}
Until ($askTeam -ne "y")
}

February 13th, 2015 1:10am

You're welcome. That's basically what I was suggesting up in my first post, but I concede that it is easier to see with an example.
Free Windows Admin Tool Kit Click here and download it now
February 13th, 2015 2:04am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics