Thank you Cliff, Steve and Luc for helping me out with this task.
After you have created the required LUN’s on your storage array and mapped the LUN’s to your ESXi hosts, at this stage you may use PowerCLI to add these new LUNs as VMFS datastore’s.
1. Connect to vCenter:
Connect-VIServer -Server “vcenter_IP” -User UserName -Password Password
If you need to retreive the Cluster and Host names:
look up the Cluster Name:
Get-Cluster
look up the Host names in the cluster:
Get-Cluster ‘Cluster Name’ | Get-VMHost | Select Name
2. Retrieve the Canonical Name(s):
In order to use the New-Datastore cmdlet we require the ‘Canonical Name’ parameter associated with each LUN. In this example we will use the Get-SCSILun cmd to return the ‘Canonical Name’, Capacity and Runtime name in order for us to match the unique naa with the correct LUN#.
Get-SCSILun -VMhost “YourESXiHostname” -LunType Disk | Select CanonicalName,CapacityGB,runtimename
The naa’s and runtime names are returned, but we want to order the output by the runtime names ‘LUN ID’ ‘vmhba1:C0:T0:L#’. Uning the | sort-object runtimename parameter the LUNs are ordered, but the double digit’s are not recognised:
I was able to sort the SCSI LUN’s by RuntimeName including the double digit’s ‘vmhba1:C0:T0:L##’ using the following script provided by Luc:
Get-ScsiLun -VMHost “YourESXiHostname” -LunType disk |
Select RuntimeName,CanonicalName,CapacityGB |
Sort-Object -Property {$_.RuntimeName.Split(‘:’)[0],
[int]($_.RuntimeName.Split(‘:’)[1].TrimStart(‘C’))},
{[int]($_.RuntimeName.Split(‘:’)[2].TrimStart(‘T’))},
{[int]($_.RuntimeName.Split(‘:’)[3].TrimStart(‘L’))}
3. Adding the New VMFS Datatore’s:
vSphere PowerCLI provides the New-Datastore cmd to create the VMFS Datastore(s):
New-Datastore -VMHost “YourESXiHostname” -Name ‘VMFS_Datastore_1’ -Path naa.60000xxxxxxxxxxxxxxxxx36 -vmfs
New-Datastore -VMHost “YourESXiHostname” -Name ‘VMFS_Datastore_2’ -Path naa.60000xxxxxxxxxxxxxxxxx42 -vmfs
4. Rescanning the Cluster for the New VMFS Datastores:
To rescan the entire cluster of ESXi host’s for the VMFS volumes added, issue the following PowerCLI cmd to rescan all the ESXi hosts HBA’s:
Get-Cluster -name “YourClusterName” | Get-VMhost | Get-VMHostStorage –RescanAllHBA
5. List all the Newly Created VMFS Datastore’s:
Get-Datastore -VMhost “YourESXiHostname”
Get-Cluster -name “ClusterName” | Get-VMhost | Get-Datastore
******************************************************************************************
Script to Automate Adding VMFS Volumes
Here is a script which Steve Keating kindly helped put together which scans for the Host LUN ID and then attributes the $LUN_ parameter to the ‘Canonical Name’. This greatly simplifies the process of adding large quantities of VMFS datastore’s.
For example if we know the following:
Host LUN ID ‘1’ will equal ‘VMFS_Datastore_1’
Host LUN ID ‘2’ will equal ‘VMFS_Datastore_2’
etc…
The following example script will automatically create four VMFS datastore’s and scan the Hosts HBA’s:
###VMFS_Datastore_1###
$LUN_1 = Get-SCSILun -VMhost YourESXiHostname -LunType Disk | Where-Object {$_.runtimename -like "*L1"} | Select CanonicalName,runtimename
$LUN_1 = $LUN_1 | Select CanonicalName
$LUN_1 = $LUN_1 -replace "@{CanonicalName=", ""
$LUN_1 = $LUN_1 -replace "}", ""
$LUN_1
New-Datastore -VMHost YourESXiHostname -Name VMFS_Datastore_1 -Path $LUN_1 -Vmfs
###VMFS_Datastore_2###
$LUN_2 = Get-SCSILun -VMhost YourESXiHostname -LunType Disk | Where-Object {$_.runtimename -like "*L2"} | Select CanonicalName,runtimename
$LUN_2 = $LUN_2 | Select CanonicalName
$LUN_2 = $LUN_2 -replace "@{CanonicalName=", ""
$LUN_2 = $LUN_2 -replace "}", ""
$LUN_2
New-Datastore -VMHost YourESXiHostname -Name VMFS_Datastore_2 -Path $LUN_2 -Vmfs
###VMFS_Datastore_3###
$LUN_3 = Get-SCSILun -VMhost YourESXiHostname -LunType Disk | Where-Object {$_.runtimename -like "*L3"} | Select CanonicalName,runtimename
$LUN_3 = $LUN_3 | Select CanonicalName
$LUN_3 = $LUN_3 -replace "@{CanonicalName=", ""
$LUN_3 = $LUN_3 -replace "}", ""
$LUN_3
New-Datastore -VMHost YourESXiHostname -Name VMFS_Datastore_3 -Path $LUN_3 -Vmfs
###VMFS_Datastore_4###
$LUN_4 = Get-SCSILun -VMhost YourESXiHostname -LunType Disk | Where-Object {$_.runtimename -like "*L4"} | Select CanonicalName,runtimename
$LUN_4 = $LUN_4 | Select CanonicalName
$LUN_4 = $LUN_4 -replace "@{CanonicalName=", ""
$LUN_4 = $LUN_4 -replace "}", ""
$LUN_4
New-Datastore -VMHost YourESXiHostname -Name VMFS_Datastore_4 -Path $LUN_4 -Vmfs
###Scan Cluster Hosts HBA’s###
Get-Cluster -name "YourClusterName" | Get-VMhost | Get-VMHostStorage –RescanAllHBA
Hi there! Great job with these scripts, I found them very useful, thanks for sharing.
Just one question: What if we need to automatically create a datastore by scanning new LUNs and assigning a consecutive number to its name instead of fixed names? I mean for example Datastore_1, Datastore_2, and so on as they’re created. I guess there’s some more scripting involved but I’m a newbie and have a customer who wants that. Thanks a lot for any input.
Wow,
This is a great piece of code/info. I just finished my 3rd move to new storage LUNS and this made the process so much easier. Clear, clean and concise!
I don’t know why it is so hard to find good automation scripting for some of the more off the path VMware tasks, but thank you for this.
Hi Rob. Glad to be of help.
David, I really like the work you have done here and I want to use it but need to make some adjustments. Can I run those questions by you via email?
David, this is a great article and script! Thank you very much! I was wondering how the cmdlet would be different if we’re mounting a datastore in a recovery or failover scenario. In other words, if I’m not creating a new VMFS datastore but want to mount an existing one instead? We are looking for a way to help automate recovery. Thanks!
The Script looks pretty good but it seems not to be working for me.
At the Point where the CanonicalName is edited somethign is not working.
If i take a look at $Lun_1 after removing “@{CanonicalName=” the output looks like this:
#TYPE System.String
Length
38
Anyone has an Idea what to do?
By the way i’m using this with the CapacityGB instead of the RuntimeName, which also works.
this was pretty helpful, and i figured that if i knew only 1 new LUN was recently created, then i could sort and pick the last item with:
Get-ScsiLun -VMHost “myhostname” -LunType Disk | Sort-Object -Property {$_.RuntimeName.Split(‘:’)[0],[int]($_.RuntimeName.Split(‘:’)[1].TrimStart(‘C’))},{[int]($_.RuntimeName.Split(‘:’)[2].TrimStart(‘T’))},{[int]($_.RuntimeName.Split(‘:’)[3].TrimStart(‘L’))} | Select-Object CanonicalName,CapacityGB,RuntimeName,Model,Vendor,VsanStatus -Last 1 | Format-Table -AutoSize
But even more useful is this function: http://www.lucd.info/2013/01/15/find-free-scsi-luns/
hello , good script but I have a question , is there any parameter to add to specify the vmfs version because I have some esx 6.0 and others 6.5 .. (vmfs 5 and 6) so I have to add newdatastore depend the version of the esx on the cluster. thanks!!