Add Virtual Switch and Portgroups for in-guest iSCSI Connectivity to vSphere Hosts

Posted: May 28, 2014 in Networking, Scripts, Storage, VMware

Had a client who required in-guest iSCSI connections. After reviewing the physical NICs that were going to be available on each host, I created the following script to automate the creation of the virtual switch with associated uplinks and two portgroups.

Along with creating the basics, it also reconfigures the portgroups to override the virtual switch load balancing policy, use explicit failover and then ensure each portgroup only ever uses one of the two physical NICs. It’s also worth noting that these uplinks were connected to access ports at the physical layer, hence no VLAN tagging.

Connect-VIServer LocalHost

# CSV file that specifies VMHost, vSwitch, VMNIC1, VMNIC2, NumPorts, PG1 and PG2
$vSwitchInfo = Import-CSV C:\Scripts\vswitch.csv

ForEach ($vSwitch in $vSwitchInfo) {

$NewvSwitch = New-VirtualSwitch -Host $vSwitch.VMHost -Name $vSwitch.vSwitch -NumPorts $vSwitch.NumPorts -MTU 9000 -Nic $vSwitch.VMNIC1, $vSwitch.VMNIC2
$PG1 = New-VirtualPortGroup -VirtualSwitch $NewvSwitch -Name $vSwitch.PG1
$PG2 = New-VirtualPortGroup -VirtualSwitch $NewvSwitch -Name $vSwitch.PG2
Get-NicTeamingPolicy $PG1 | Set-NicTeamingPolicy -InheritLoadBalancingPolicy $False -LoadBalancingPolicy ExplicitFailover -InheritFailoverOrder $false
Get-NicTeamingPolicy $PG1 | Set-NicTeamingPolicy -MakeNicActive $vSwitch.VMNIC1 -MakeNicUnused $vSwitch.VMNIC2
Get-NicTeamingPolicy $PG2 | Set-NicTeamingPolicy -InheritLoadBalancingPolicy $False -LoadBalancingPolicy ExplicitFailover -InheritFailoverOrder $false
Get-NicTeamingPolicy $PG2 | Set-NicTeamingPolicy -MakeNicActive $vSwitch.VMNIC2 -MakeNicUnused $vSwitch.VMNIC1

}

Disconnect-VIServer * -Confirm:$False

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s