Tuesday, February 15, 2011

Mount all Datastores with Powershell

A couple of weeks ago, I ran into a problem.  We were re-designing our networking in the datacenter so that all ESX hosts exclusively used 10GbE interfaces.  Since our underlying foundation is Layer 2 Nexus 5k switches, we have become huge fans of twinax cabling, and not having to buy additional SFP's to make the interfaces compatible with other gear.

When we cabled the first one up, I figured I would just reconfigure the networking manually since the vmnic# assignments would be different, and then use Host Profiles to reconfigure the rest of the host to mount all NFS datastores, set cpu/mem limits reservations, and configure firewall rules.



What I learned were some hard lessons about Host Profiles.  They've got a long way to go.  If you're adding an identically cabled host to an already existing cluster, it works like a champ.  (of course, it won't do some discreet things like set vm swaplife location)  It would not allow me to NOT configure networking.  And what I mean by that is, it would not give me any sort of advanced route to get the host online, configure networking properly, and then tell the host profile to completely disregard networking.  I ever went as far as attempting to create the Host Profile, and then manually removing all networking configuration from within the profile.  When I tried to save it....

"Sorry, you must configure networking for this profile..."


For those of you that play WoW, this was a /facepalm moment.


So, I was out of ideas.  How in the world could I get all my datastores mounted?  The rest of my host config was simple enough, but I didn't want to sit there and manually mount 50+ different datastores.


I reached out to a couple of people, and ultimately Erick Moore (@erickMoore) came up with the solution.


Powershell.


If you don't know Erick, you need to be following his stuff.  He is one of the people who have taken a lead in Powershell scripting for NetApp and virtual environments, and threw a script together quickly for me to accomplish this task.  Here is the script:



------------------------------------------------------------------------------------------------------------


$vcenter = "vcentername"
$dataCenter = "Your Datacenter"
$vSphereHost = "FQDNHostName"

Connect-VIServer $vcenter
$vSphereHost = Get-VMHost $vSphereHost

#----{ Get all NFS mounts in vSphere datacenter
#

$nfsDS = get-datastore -Datacenter $dataCenter | where {$_.Type -eq "NFS"} | get-view | select Name,@{n="url";e={$_.summary.url}}


#----{ Parse NFS mount info and mount all datastores to specififed vSphere host
#

Foreach ($ds in $nfsDS) {

 $nfsPath = $null
 $i = 4
 $nfsInfo = $ds.url.split("/")
 $dsName = $ds.Name
 $nfsHost = $nfsInfo[2]
 Do { $nfsPath = $nfsPath + "/" + $nfsInfo[$i] ; $i ++} Until ( $i -eq ($nfsInfo.count - 1) )
 
 New-Datastore -Nfs -VMHost $vSphereHost -Name $dsName -Path $nfsPath -NfsHost $nfsHost -WhatIf

}

----------------------------------------------------------------------------------

Basically what happens here is the script grabs all datastores mounted at the "datacenter" level (this could even be modified to the "cluster" level) and attempts to mount each of them to your new host.

Simple. 
Brilliant.

Erick, thanks a million for getting me over this hump.  Worked like a champ.

-Nick

No comments:

Post a Comment