I threw this together a while ago, it will help configure iSCSI on a VM host using PowerCLI. Note that once you do a find/replace to set your iSCSI network (e.g. 192.168.0.0/24 192.168.1.0/24), all you’ll need to change is the $thisHost, $lastOctet, and $targets values before running.
Requires magic from Jonathan Medd’s Blog (for the Set-VMHostiSCSIBinding cmdlet).
# This configures iSCSI from scratch!
# Set the specific information for this host
$this Host = Get-VMHost dc2server03 *
$lastOctet = 62
$iSCSI_addr1 = "192.168.0. $lastOctet "
$iSCSI_addr2 = "192.168.1. $lastOctet "
# Get the iSCSI dvSwitch Port Groups
$iSCSI_dvPG1 = Get-VDPortgroup "iSCSI dvPG1"
$iSCSI_dvPG2 = Get-VDPortgroup "iSCSI dvPG2"
$iSCSI_dvSw = Get-VirtualSwitch -name "UCS iSCSI dvSwitch"
# Create a new host network adapter for this host on both dvPGs
New-VMHostNetworkAdapter -VMHost $this Host -PortGroup $iSCSI_dvPG1 -IP $iSCSI_addr1 -SubnetMask 255.255.255.0 -Mtu 9000 -VirtualSwitch $iSCSI_dvSw
New-VMHostNetworkAdapter -VMHost $this Host -PortGroup $iSCSI_dvPG2 -IP $iSCSI_addr2 -SubnetMask 255.255.255.0 -Mtu 9000 -VirtualSwitch $iSCSI_dvSw
# Setup the iSCSI Binding
$this Host | Set-VMHostiSCSIBinding -HBA vmhba32 -VMKernel "vmk1"
$this Host | Set-VMHostiSCSIBinding -HBA vmhba32 -VMKernel "vmk2"
# Configure the iSCSI targets
$targets = "192.168.0.10" , "192.168.0.20" , "192.168.1.10" , "192.168.1.20"
$hba = $this Host | Get-VMHostHba -Type iScsi | Where { $_ . Model -eq "iSCSI Software Adapter" }
foreach ( $target in $targets ){
if ( Get-IScsiHbaTarget -IScsiHba $hba -Type Send | Where { $_ . Address -cmatch $target }){
Write-Host "The target $target does exist on $this Host" -ForegroundColor Green
}
else {
Write-Host "The target $target doesn't exist on $this Host" -ForegroundColor Red
Write-Host "Creating $target " -ForegroundColor Yellow
New-IScsiHbaTarget -IScsiHba $hba -Address $target | Out-Null
Write-Host "done..." -ForegroundColor Green
}
}
# Rescan to find iSCSI datastores
$this Host | Get-VMHostStorage -RescanAllHba
# Just a little clean up, rename any default local datastores
$this Host | get-datastore datastore * | set-datastore -name " $( $this Host.name.Substring ( 0 , $this Host.Name.IndexOf ( "." ) ))_boot"