Script to create iSCSI vSwitch in VMware ESXi

Script to create iSCSI vSwitch in VMware ESXi

# Leia este post em português

This script connect to vCenter Server and create a vSwitch Standard for using iSCSI network in three VMware ESXi hosts. Are used two network interfaces and three VMKernels (iSCSI01, iSCSI02 and Storage Heartbeat). To run the script you need the VMware PowerCLI, hostname or IP and administrator access credentials vCenter Server, hostname or IP of the ESXi servers, nomenclature for the vSwitch, VMKernels IP and iSCSI network. You must also define which network interfaces assigned to this vSwitch, besides the possibility of set the MTU. The script also enables a network interface for a VMkernel, while the other interface is unused.

script-vswitch-iscsi-2.jpg

script-vswitch-iscsi-3.jpg

Below are some details of the script:

######################### General Setup ###########################

$vcenter_name = "rc-vc01.rc.local"

$vswitch_name = "iSCSI"

$portgroup1_name = "iSCSI01"

$portgroup2_name = "iSCSI02"

$portgroup3_name = "StorageHeartbeat"

$device_nic1 = "vmnic2"

$device_nic2 = "vmnic3"

$mtu_value = "9000"

$network_mask = "255.255.255.0"

script-vswitch-iscsi-4.jpg

########################## VMware ESXi 1 ##########################

$esxi1_name = "rc-esxi01.rc.local"

$ip_portgroup1_esxi1 = "10.0.0.11"

$ip_portgroup2_esxi1 = "10.0.0.21"

$ip_portgroup3_esxi1 = "10.0.0.31"

# Conect to vCenter Server

Connect-VIServer $vcenter_name

# Create vSwitch in ESXi Hosts

Foreach ($vmhost in (get-vmhost))

{

New-VirtualSwitch -VMHost $vmhost -Name $vswitch_name -Nic $device_nic1,$device_nic2 -MTU $mtu_value

}

########################## VMware ESXi 1 ##########################

New-VMHostNetworkAdapter -VMHost $esxi1_name -PortGroup $portgroup1_name -VirtualSwitch $vswitch_name -IP $ip_portgroup1_esxi1 -SubnetMask $network_mask -FaultToleranceLoggingEnabled:$false -ManagementTrafficEnabled:$false -VsanTrafficEnabled:$false -VMotionEnabled:$false -Mtu $mtu_value

New-VMHostNetworkAdapter -VMHost $esxi1_name -PortGroup $portgroup2_name -VirtualSwitch $vswitch_name -IP $ip_portgroup2_esxi1 -SubnetMask $network_mask -FaultToleranceLoggingEnabled:$false -ManagementTrafficEnabled:$false -VsanTrafficEnabled:$false -VMotionEnabled:$false -Mtu $mtu_value

New-VMHostNetworkAdapter -VMHost $esxi1_name -PortGroup $portgroup3_name -VirtualSwitch $vswitch_name -IP $ip_portgroup3_esxi1 -SubnetMask $network_mask -FaultToleranceLoggingEnabled:$false -ManagementTrafficEnabled:$false -VsanTrafficEnabled:$false -VMotionEnabled:$false -Mtu $mtu_value

Get-VirtualPortGroup -VMHost $esxi1_name -Name $portgroup1_name | Get-NicTeamingPolicy | Set-NicTeamingPolicy  -MakeNicActive $device_nic1 -MakeNicUnused $device_nic2

Get-VirtualPortGroup -VMHost $esxi1_name -Name $portgroup2_name | Get-NicTeamingPolicy | Set-NicTeamingPolicy  -MakeNicActive $device_nic2 -MakeNicUnused $device_nic1

You can edit the script to use more or fewer hosts, network interfaces and VMKernels. Feel free to edit it according to your need.

Script download: EN-Create-vSwitch-iSCSI.ps1

References:

Version history
Revision #:
1 of 1
Last update:
‎03-13-2015 07:39 AM
Updated by: