PowerCLI – Setup Host networking and storage ready for ISCSI LUNs

Useful script. Additional details and screen shots can be found using the following link

Script below provided by @Saintdle

( All scripts should be tested in a Lab environment only )

#Setup which host to target
$VMhost = ‘hostname’

#Create vSwitch2 for storage, add vmnics, add two vmkernels with Storage IPs, setup NIC teaming (based on the fact you probably have vSwitch0 for mgmt and vSwitch1 for VM traffic)

$vswitch2 = get-vmhost $VMhost | new-virtualswitch -Name vSwitch2 -Nic ‘vmnic2′,’vmnic5’ -Mtu 9000 -NumPorts 120

New-VMHostNetworkAdapter -VMhost $VMhost -virtualswitch $vswitch2 -portgroup iSCSI_ESX_01 -ip IP_ADDR -subnetmask SUBNET_MASK -Mtu 9000

New-VMHostNetworkAdapter -VMhost $VMhost -virtualswitch $vswitch2 -portgroup iSCSI_ESX_02 -ip IP_ADDR -subnetmask SUBNET_MASK -Mtu 9000

Get-VirtualPortGroup -VMhost $VMhost -virtualswitch $vswitch2 -Name iSCSI_ESX_01 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic2 -MakeNicUnused vmnic5

Get-VirtualPortGroup -VMhost $VMhost -virtualswitch $vswitch2 -Name iSCSI_ESX_02 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic5 -MakeNicUnused vmnic2

#Create Software iSCSI Adapter

get-vmhoststorage $host | set-vmhoststorage -softwareiscsienabled $True

#Get Software iSCSI adapter HBA number and put it into an array

$HBA = Get-VMHostHba -VMHost $VMHost -Type iSCSI | %{$_.Device}

#Set your VMKernel numbers, Use ESXCLI to create the iSCSI Port binding in the iSCSI Software Adapter

$vmk1number = ‘vmk1’
$vmk2number = ‘vmk2’
$esxcli = Get-EsxCli -VMhost $VMhost
$Esxcli.iscsi.networkportal.add($HBA, $Null, $vmk1number)
$Esxcli.iscsi.networkportal.add($HBA, $Null, $vmk2number)

#Setup the Discovery iSCSI IP addresses on the iSCSI Software Adapter

$hbahost = get-vmhost $VMhost | get-vmhosthba -type iscsi
new-iscsihbatarget -iscsihba $hbahost -address IP_ADDR

#Rescan the HBA to discover any storage
get-vmhoststorage $VMhost -rescanallhba -rescanvmfs