I recently had the daunting task of adding several hosts that had lots of NFS volumes attached, and adding them manually in the GUI would have taken ages.
To expedite this I wrote a little Powershell foreach statement that copied the volumes from one host to another. Granted I could have made this much smarter but it is one of those “quick and dirty” bits of script that got the job done. So how was it done?The first step was to confirm which commands would help with this. Luckily PowerCLi includes the Get-Datastore command.
I connected to the vCenter using the Connect-VIServer command
Connect-VIServer "vCenter"
I then ran the Get-Datastore command with a filter to only bring back NFS volumes that were accessible
get-cluster | Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"}
Once i had confirmed this command worked I decided to knock together a little foreach statement that would take volumes found on one host and copy them over to the other one
foreach ($datastore in (Get-vmhost Host1 | Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"})){ New-Datastore -vmhost Host2 -Nfs -Name $datastore.Name -Path $datastore.RemotePath -NfsHost $datastore.RemoteHost }
This resulted in the volumes being copied across, which in my lab environment only included two volumes
Name FreeSpaceGB CapacityGB ---- ----------- ---------- SWAP 3,114.036 3,595.917 ISOs 3,114.036 3,595.917
However in the environment I was actually working, it was considerably more so it saved a fair bit of time.
One thing to remember is a clean install of ESXi will have a limit of NFS Volumes set at 8 so remember to change the advanced setting of NFS.MaxVolumes to the number you require with the maximum being 256 on ESXI 5.x and above.
The KB article from VMware on the Max Volumes is here
One additional note is there is a great powerCLI reference sheet here. Always worth keeping a copy to hand.
Enjoy
Leave a Reply