4 Replies Latest reply on Oct 9, 2019 11:38 AM by TryllZ

    Testing an IF condition..

    TryllZ Enthusiast

      Hi,

       

      I have this CSV file that I'm using for deployment.

       

      I have the following script.

       

      foreach ($val in $csvFile)

      {

      $esxiHosts = $val.Hosts

      $clusterName = $val.Cluster

      $virtualSwitch = $val.Switch

      $nic = $val.NIC

      $vmK = $val.VMK

      $ip = $val.IP

         $gSwitch = Get-VMHost -Name $esxiHosts | TRYING TO ADD AN IF CONDITION HERE | New-VirtualSwitch -Name $virtualSwitch -Nic $nic

         $nAdapter = Get-VMHostNetworkAdapter -VMHost $esxiHosts -Physical -Name $nic

         $gSwitch | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nAdapter -Confirm:$false

         New-VMHostNetworkAdapter -VMhost $esxiHosts -PortGroup $vmK -IP $ip -subnetmask 255.255.255.0 -VirtualSwitch $gSwitch -confirm:$false

      }

       

      I want to put an IF condition to check if a virtual switch exists, if it does then just just add its associated NIC to it and if not then create the switch and add an NIC to it.

       

      I just could not find a way to do this, neither could I find a reference of an if condition in PowerCLI.

       

      I tried the following as a test, but did not succeed.

       

      get-virtualswitch

       

      Name                           NumPorts   Mtu   Notes

      ----                           --------   ---   -----

      vSwitch0                       2560       1500

      NFSSwitch                      2560       1500

      vSANSwitch                     2560       1500

      vSwitch0                       2560       1500

      NFSSwitch                      2560       1500

      vSANSwitch                     2560       1500

      vSwitch0                       2560       1500

      NFSSwitch                      2560       1500

      vSANSwitch                     2560       1500

      vSwitch0                       2560       1500

      NFSSwitch                      2560       1500

      iSCSISwitch                    2560       1500

      vSwitch0                       2560       1500

      NFSSwitch                      2560       1500

      iSCSISwitch                    2560       1500

       

      if ($_.Name -eq "NFSSwitch") {write-host "Switch Exists"} else {write-host "NA"}

      NA

       

      Any help is appreciated.

        • 1. Re: Testing an IF condition..
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          Another way to do this is with a try-catch construction.

          Something like this

           

          foreach ($val in $csvFile) {

              $esxiHosts = $val.Hosts

              $clusterName = $val.Cluster

              $virtualSwitch = $val.Switch

              $nic = $val.NIC

              $vmK = $val.VMK

              $ip = $val.IP

              try{

                  $gSwitch = Get-VirtualSwitch -Name $virtualSwitch -VMHost $esxiHosts -ErrorAction Stop

              }

              catch{

                  $gSwitch = New-VirtualSwitch -Name $virtualSwitch -VMHost $esxiHosts

              }

              $nAdapter = Get-VMHostNetworkAdapter -VMHost $esxiHosts -Physical -Name $nic

              $gSwitch | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nAdapter -Confirm:$false

              New-VMHostNetworkAdapter -VMhost $esxiHosts -PortGroup $vmK -IP $ip -subnetmask 255.255.255.0 -VirtualSwitch $gSwitch -confirm:$false

          }

          • 2. Re: Testing an IF condition..
            TryllZ Enthusiast

            Thanks LucD,

             

            I just wanted to know how the try and catch works in context of PowerCLI.

             

            This is what I'm understanding from the script you have provided.

             

            The following line is executed

             

                  Try {  $gSwitch = Get-VirtualSwitch -Name $virtualSwitch -VMHost $esxiHosts -ErrorAction Stop }

             

            and if a switch exists (meaning an error is thrown) then the catch is skipped and the following is executed.

             

                    $nAdapter = Get-VMHostNetworkAdapter -VMHost $esxiHosts -Physical -Name $nic

                    $gSwitch | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nAdapter -Confirm:$false

                    New-VMHostNetworkAdapter -VMhost $esxiHosts -PortGroup $vmK -IP $ip -subnetmask 255.255.255.0 -VirtualSwitch $gSwitch -confirm:$false

             

            if a switch does not exists (meaning NO errors) then the catch part is executed followed by the above 3 lines of code.

             

            Do correct my understanding if I'm mistaken.

             

            Thank You

            • 3. Re: Testing an IF condition..
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              The concept is as follows:

              • execute the code in the try-block
                • if there is a terminating error (forced in this case by the ErrorAction), the code in the catch block is executed
              • in this particular code, the try-block tries to Get the virtualswitch.
                • If it doesn't exist, the ErrorAction forces a terminating error
                • The catch-block creates a new virtualswitch
              • In both cases, the virtualswitch object is stored in the $gSwitch variable
              • 4. Re: Testing an IF condition..
                TryllZ Enthusiast

                Thanks as always, appreciate the help.