Which Loop To Use?!

Tagged: , ,

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of asecor asecor 1 year, 11 months ago.

  • Author
    Posts
  • #27886
    Profile photo of asecor
    asecor
    Participant

    I think I am trying to run before I can walk but I am trying to learn PS by creating a solution to a problem I have – best way to learn right?!

    This is what I am trying to accomplish (utilising PowerCLI cmdlets as well):

    1. connect to esxihost
    1.1 specify (read-host) vswitch0 nics
    1.2 specify (read-host) vswitch1 nics
    2. check if vswitch0 exists
    2.1 if exists create new portgroups
    2.2 if it doesn't exists create vswitch0
    2.3 add nics specified in 1.1 to vswitch0
    2.4 go to 2.1
    3. check if vswitch1 exists
    3.1 if exists create new portgroups
    3.2 if it doesn't exists create vswitch1
    3.3 add nics specified in 1.1 to vswitch1
    3.4 go to 3.1

    `
    $vmhost = Connect-VIServer 10.0.0.114 -User root -Password Password1 #connet to esxi host
    $vswitch = Get-VirtualSwitch #get vswitches
    [array]$vswitch0nics = (Read-Host "Enter vSwitc0 NICs - comma separated")#.Split(",") | %{$_.Trim()} #specify (read-host) vswitch0 nics
    [array]$vswitch1nics = (Read-Host "Enter vSwitc1 NICs - comma separated")#.Split(",") | %{$_.Trim()} #specify (read-host) vswitch1 nics
    
    
    if ($vswitch -notmatch "vSwitch0") #check if vswitch0 exists & if it doesn't exists create vswitch0 adding nics specified on line 3 to it
    {
        New-VirtualSwitch -VMHost $vmhost -Name vSwitch0 -Nic $vswitch0nics
    }
    elseif ($vswitch -match "vSwitch0") #if switch0 exists create new portgroups 
    {
        New-VirtualPortGroup -VirtualSwitch $vswitch -Name Servers -VLanId 22 -Server $vmhost
        New-VirtualPortGroup -VirtualSwitch $vswitch -Name Development -VLanId 122 -Server $vmhost
        New-VirtualPortGroup -VirtualSwitch $vswitch -Name Testing -VLanId 143 -Server $vmhost
    }
    elseif ($vswitch -notmatch "vSwitch1") #check if vswitch1 exists & if it doesn't exists create vswitch1 adding nics specified on line 4 to it
    {
        New-VirtualSwitch -VMHost $vmhost -Name vSwitch1 -Nic $vswitch1nics
    
    }
    elseif ($vswitch -match "vSwitch1") #if switch1 exists create new portgroups 
    {
        New-VirtualPortGroup -VirtualSwitch $vswitch -Name vMotion -VLanId 200 -Server $vmhost
    }
    else
    {
    Write-host "Finished"
    }
    ` 

    I did read that if you have a lot of ifs you should use switches – and I am trying to figure those out.

    But I am also trying to figure out how to implement "code blocks?"/loops/conditional loops – something that replaces "go to".

    Thanks in advance!

  • #27907
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    You want to look at functions as the replacement for 'go to'. You implement the code in a function block:

    #declare the function
    function myFirstFunction {
        Write-Host "Hello world!"
    }
    #call the function
    myFirstFunction
    

    Every time you need to use the code in the function, you call the function by name. The code within the function will not do anything until you call the function.

    Functions can also be called with parameters. This means that you can pass data to them and have the function use this data in its processing.

    function mySecondFunction {
        param (
        $text = 'Hello world'
        )
    
        Write-Host $text
    
    }
    
    #call the function without a parameter
    mySecondFunction
    
    #call the function with a parameter
    mySecondFunction -text 'Hello Universe!'
    

    You can, of course, call the function based on a condition. e.g. you might find it useful to have a createSwitch function and use it in your script like this:

    if ($vswitch -notmatch "vSwitch0") {
    
        createSwitch 
    
    }
    
  • #28786
    Profile photo of asecor
    asecor
    Participant

    Thanks Matt. That was exactly what I was looking for!

    (apologies for the delayed reply!)

You must be logged in to reply to this topic.