PowerCLI: Mass provision datastore’s
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 !
PowerCLI: One-liners last 10 VMs created and removed PowerCLI: APIs made easy “Project Onyx”











Hey there,
quick ?-Under 4.0 the nomenclature seems to have changed a bit-i.e. from ‘vmhba1:4:0′ on a 3.5 host to ‘naa.600601600a402300fc3bb01aac1fdf11′ for the Identifier field within VC. Has the Cmdlet syntax changed as well?
What format should the LUN ids be within the spreadsheet? Do you have a sample copy of your csv file?