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.
The concept is as follows:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Another way to do this is with a try-catch construction.
Something like this
$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
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
The concept is as follows:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks as always, appreciate the help.