VMware Cloud Community
TryllZ
Expert
Expert
Jump to solution

Testing an IF condition..

Hi,

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

pastedImage_0.png

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.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
TryllZ
Expert
Expert
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
TryllZ
Expert
Expert
Jump to solution

Thanks as always, appreciate the help.

0 Kudos