Dean Lewis

PowerCLI to setup hosts networking and storage ready for iSCSI LUNS

Discussion created by Dean Lewis on Jan 26, 2016
Latest reply on Feb 1, 2016 by Matt Allford



I am by no means a scripting person, but the other day I had quite a few greenfield hosts to deploy, so I decided to use powercli to make life easier, and do the job quicker. Admittedly it took longer for me to actually find all the commands, but still, they can be used in the future.


Where in red you need to edit to fit your environment


#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)

$vs2 = get-vmhost $VMhost | new-virtualswitch -Name vSwitch2 -Nic 'vmnic2','vmnic5' -Mtu 9000 -NumPorts 120

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

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

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

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


#Create Software iSCSI Adapter

get-vmhoststorage $VMhost | 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


If anyone wants to improve the above, reach out, I put this together in an afternoon/evening during the install I was on. So it was rushed and best effort.

This will also appear on my blog as well