Wednesday, December 9, 2015

Add datastores with lun id:method 2

So i have this headache of automating the addition of luns to a cluster. Finding the naa id and adding them one by one as described here. I wanted to be able to figure out a way to add them using the lun id.
First we want to perform this activity to the 1st host (or any one of the host of the cluster) and then we can just rescan all hosts of the cluster to make the luns visible to all hosts of the cluster.
so let us find the 1st host of the cluster shall we?
#enter the name of the cluster
let us assume the name of the cluster as SAN


#enter the name of the cluster
$cluster   = ""

Now let us list all the hosts of the cluster


(get-cluster $cluster | get-vmhost)
 and the results will be something like
Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB Version
----                 --------------- ---------- ------ ----------- -----------   -------------   ------------- -------
vb-host01.dummy... Maintenance     PoweredOn      20          97       59980           3.676         128.932   5.5.0
vb-host02.dummy... Maintenance     PoweredOn      20          71       59980           3.702         128.932   5.5.0
vb-host03.dummy... Maintenance     PoweredOn      20          71       59980           3.714         128.932   5.5.0
vb-host04.dummy... Maintenance     PoweredOn      20          72       59980           3.693         128.932   5.5.0
vb-host05.dummy... Maintenance     PoweredOn      20          50       59980           3.674         128.932   5.5.0
vb-host06.dummy... Maintenance     PoweredOn      20         109       59980           3.687         128.932   5.5.0
vb-host07.dummy... Maintenance     PoweredOn      20          72       59980           3.700         128.932   5.5.0
vb-host08.dummy... Maintenance     PoweredOn      20          71       59980           3.692         128.932   5.5.0
vb-host09.dummy... Maintenance     PoweredOn      20          71       59980           3.702         128.932   5.5.0
vb-host10.dummy... Maintenance     PoweredOn      20          72       59980           3.685         128.932   5.5.0
vb-host11.dummy... Maintenance     PoweredOn      20          77       59980           3.680         128.932   5.5.0
vb-host12.dummy... Maintenance     PoweredOn      20          72       59980           3.692         128.932   5.5.0
vb-host13.dummy... Maintenance     PoweredOn      20          69       59980           3.712         128.932   5.5.0
vb-host14.dummy... Maintenance     PoweredOn      20          72       59980           3.698         128.932   5.5.0
vb-host15.dummy... Maintenance     PoweredOn      20          71       59980           3.693         128.932   5.5.0
vb-host16.dummy... Maintenance     PoweredOn      20          71       59980           3.704         128.932   5.5.0


Now please forgive the formatting here.
But we want just the first host right?
so lets do this

(get-cluster $cluster | get-vmhost)[0]
 so we will get
Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB Version
----                 --------------- ---------- ------ ----------- -----------   -------------   ------------- -------
vb-host01.dummy... Maintenance     PoweredOn      20         167       59980           3.685         128.932   5.5.0

Now once we have this let us first define the name of the datastore and the lun id which we want add like this

#enter the name of the lun id
$lunid     = ""
#enter the name of the datastore
$datastore = ""

 now let us get the naa id of that host where the lun id is $lunid

$vmhost  = (get-cluster $cluster | get-vmhost)[0]
$naa     = (Get-SCSILun -VMhost $vmhost -LunType Disk | where    runtimename -eq vmhba1:C0:T0:L$lunid).CanonicalName

and how shall we add it then?

New-Datastore -VMHost $vmhost -Name $datastore -Path $naa -vmfs

 but don't we need a way to rescan all the hosts of that cluster. Well that shouldnt be a problem.
#Rescan HBAs for a cluster on a host by host basis
foreach ($a in (get-cluster $cluster | get-vmhost)) {
Write-Host "Rescanning of HBAs $a" -ForegroundColor Yellow
$a | Get-VMHostStorage -RescanAllHba | Out-Null
Write-Host "Rescan of all HBAs on $a is complete" -ForegroundColor Green
Write-Host ""
}


  but as you can see this is not an efficient method yet. I still have to change the lun id and datastore name everytime but hey that is what leads to method 3 where you use a csv file for this. :P

I have this complete script here and that is where i am more comfortable in keeping it updated.