Virtual Geek

Tales from real IT system administrators world and non-production environment

Add a VMFS datastore using VMware PowerCLI

This article's completely focus is on VMware PowerCLI command line to add or create new VMFS datastore, You can check Create a VMFS Datastore in the vSphere Client how The same task is performed using vSphere Client (HTML5). To start first Import Vmware.PowerCLI module on to PowerShell and log in to either vCenter or ESXi Server where you want to add VMFS datastore.

vmware vSphere powercli powershell module vmware.powercli Import-Module Connect-VIServer Administrator@vsphere.local credential vcenter esxi vcsa datastore storage automation.png

#Login to vCenter Server
Remove-Module -Name Hyper-V
Import-Module -Name VMware.PowerCLI
Connect-VIServer -Server -User administrator@vsphere.local -Password P@ssw0rd

Next I have defined ESXi server name and HBA model (This is Host bus adapter card [Storage Controller], Where disks/LUNs are attached either locally or shared from storage server).

I am storing the information about ESXi server in the variable $esxi using Get-VMhost so I can use the information again and again. If you are receiving error while running this command, it is because of conflicts, you can resolve the error using instructions on PowerCLI Get-VMhost The operation on computer failed The WinRM client cannot process the request.

Identifying the information is key, know the information about HBA adapter model available on ESXi server using Get-VMHostHba and identify datastores with Get-Datastore. I have only one physical hba adapter and a datastore.

Once I have gathered all the above information I require last list about how many scsi LUNs (disks storage) are attached to the ESXi server on HBA adapter with Get-ScsiLun. As I am going to attach the raw fresh non assign LUN the collected information is handy to identify before and after result to check which LUNs are added to make less chances of errors.

Powershell vmware vsphere vcenter powercli Get-VMHost Get-VMHostHba Get-Datastore Get-ScsiLun storage adapter hba scsi controller parallelScsi canonicalName model.png

#Define your environment
$esxiServer = ''
$hbaModel =  'PVSCSI SCSI Controller'

#Esxi server information
$esxi = Get-VMhost $esxiServer

#Get HBA storage adapter information
$hostHBA =  $esxi | Get-VMHostHba | Where-Object {$_.Model -eq $hbaModel}

#Get the list of Datastore
$datastore = Get-Datastore

#Get available LUN storage devices list
$lunList = $hostHBA | Get-ScsiLun
$lunlist | Format-Table -AutoSize

Now next verify by checking which LUNs are consumed means, Datastores are created, This information I can get with small script using with combination of Datastores and LunList information. Once I have the information I have attached disk/LUN on the ESXi in the background, but it is still not reflecting in the list.

Refresh and Rescan HBA subsystem on ESXi to detect newly attached storage/LUNs with PowerCLI cmdlet Get-VMHostStorage

Once refreshed and rescanned HBA, it may take some time, rerun Get-ScsiLun. New disk/LUN will be visible, Note the new disk/LUN canonical name it will be required later to create VMFS datastore.

Verify new disk/LUN is raw or not consumed by ESXi (no VMFS file system datastore is created on it), I have identified the same by seeing there is no display name on the new disk/LUN with small script.

#Get the list of LUN on datastores are created
foreach ($lun in $lunList)
    $lun | Select-Object CanonicalName, RuntimeName, CapacityGB, @{Label='DatastoreName'; Expression={$datastore | Where-Object {$_.Extensiondata.Info.Vmfs.Extent.DiskName.Contains($lun.CanonicalName)} | Select-Object -ExpandProperty Name}}

#Rescan/Refresh Esxi Storage 
$esxi | Get-VMHostStorage -Refresh -RescanAllHba

#Get get the newly discovered LUN information
$lunList = $hostHBA | Get-ScsiLun
$lunlist | Format-Table -AutoSize

foreach ($lun in $lunList)
    $lun | Select-Object CanonicalName, RuntimeName, CapacityGB, @{Label='DatastoreName'; Expression={$datastore | Where-Object {$_.Extensiondata.Info.Vmfs.Extent.DiskName.Contains($lun.CanonicalName)} | Select-Object -ExpandProperty Name}}

Vmware vsphere powercli esxi vCenter server powershell vmware.powercli Get-VMHostStorage -RescanAllHba -Refresh SoftwareIScsiEnabled Get-ScsiLun Format-Table -Autosize .png
In this last step Get the Canonical Name of disk/Lun from the LunList earlier generated. Here it is showing suffix mpx as an identifier because I am using Local Disk. If there were shared scsi/fcoe disk/LUNs it would have shown NAA as suffix with followed by guid from storage server. 

Use New-Datastore PowerCLI cmdlet to create a datastore by providing a new datastore a name, Canonical Name of disk/LUN and VMFS File system version, I am using latest VMFS version 6.

Recheck Datastore list with Get-Datastore and Lun to Datastore mapping script. New datastore is available for provisioning.

Where-Object Format-Table -Autosize hashtable New-Datastore Get-Datastore Get-ScsiLun get-vmhost Get-vmhosthba, storage adapter hba host bus adapter.png

#Get the RuntimeName of Lun (Local as well as Remote shared storage)
$addedNewLun = $lunlist | Where-Object {$_.CanonicalName -eq 'mpx.vmhba0:C0:T2:L0'}
$addedNewLun | Format-Table -AutoSize

#Add new Datastore using RuntimeName of newly Added Lun
$datastoreName = '5gb_local_datastore'
New-Datastore -VMHost $esxi.Name -Name '5gb_local_datastore' -Path $addedNewLun.CanonicalName -Vmfs -FileSystemVersion 6

#Recheck Datastore list
$datastore = Get-Datastore

#Recheck Datastore list
$datastore = Get-Datastore

#Recheck LUN and Datastore mapping
$lunList = $hostHBA | Get-ScsiLun
foreach ($lun in $lunList)
    $lun | Select-Object CanonicalName, RuntimeName, CapacityGB, @{Label='DatastoreName'; Expression={$datastore | Where-Object {$_.Extensiondata.Info.Vmfs.Extent.DiskName.Contains($lun.CanonicalName)} | Select-Object -ExpandProperty Name}}

Download complete script New-Datastore.ps1 or download it from

Useful Articles
Part 1 - Copy or clone distributed virtual switch portgroups to standard switch portgroups - Powercli
Migrating move back from Distributed virtual switch to Standard virtual switch - VMKernal Adapter - Part 2
Migrating move back from Distributed virtual switch to Standard virtual switch - VMs (Virtual Machine) - Part 3
Part 4: Remove ESXi Host from Distributed switch and migrate to virtual switch completely
How to import default vCenter server appliance VMCA root certificate and refresh CA certificate on ESXi
How to replace default vCenter VMCA certificate with Microsoft CA signed certificate

Go Back


Blog Search

Page Views


Follow me on Blogarama