At the moment I am adding around 50 datastore’s to a host, now whilst I love using the great Virtual Infrastructure Client wizard to do this after adding two of the 50 i remembered what someone said (can’t remember who)….. If you do something more than once, script it.

So guess what I did….

The following script was used to add the datastore’s from a csv file whilst I happily worked on other things.

The csv file consisted of the following:

LUNid,Hostid
101,1
221,2
331,3
451,4
521,5
661,6

etc etc, the naming convention for my datastores is SAN_HOSThostid_LUNlunid so this was easily scripted as so:

$CSVFile = "C:\Temp\Datastores.csv"
$MYESXHost = "ESX01.virtu-al.local"
$VMHBA = "vmhba2:0:"

Connect-VIServer MYVISERVER
$CSV = Import-CSV $CSVFile
$ESXHost = Get-VMHost $MYESXHost
$ESXHost |Get-VMHostStorage -RescanAllHBA
Foreach ($Item in $CSV){
  
$HostID = $Item.HostId
  
$LunID = $Item.LunID
  
Write "Creating SAN_HOST$($HostID)_LUN$($LunID)…"
  
$ESXHost |New-Datastore -Vmfs -Name "SAN_HOST$($HostID)_LUN$($LunID)" -Path "$($VMHBA)$($HostID)"
}

The New-Datastore cmdlet also has a -BlockSizeMB parameter which can be used to specify the block size of VMFS in megabytes.

All in all it took me around 10 minutes to add 50 datastore’s which I think would have taken significantly longer through the GUI.  Yet another reason to start learning PowerCLI !