So I am no scripting master, my PowerShell knowledge is still something I want to expand. During an install last week I had a number of hosts to setup from scratch, so I decided to do this via PowerCLI, as a lot of the tasks were repetitive. Setting up the vSwitch networking and iSCSI configuration for each host
For those of you new to scripting, I’ve included screenshots to accompany the commands so you can see whats going on in the GUI.
Note: the full code without the breaks is at the end of this post
#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 the vSwitch, set Jumbo Frames
- Create the VMKernel and configure IP address, Jumbo Frames, Override switch failover order for iSCSI Port Binding
#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}
- Create the iSCSI Software Adapter
#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)
- Configure iSCSI Port binding within iSCSI Software Adapter
#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
- Configure iSCSI Discovery IP Address for Storage device / SAN
#Rescan the HBA to discover any storage get-vmhoststorage $VMhost -rescanallhba -rescanvmfs
- Rescan storage
I can already see some places where I can simplify the script, and keep all necessary parameters at the top for easy editing between hosts, such as the IP addresses. So hopefully in future I’ll get more time to edit and refine this code.
Here it is in one big copy friendly area.
#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 $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
Regards
Dean Follow @saintdle