Adding NFS Datastores to Multiple vCenter or ESX Hosts – powershell script

Adding NFS Datastores to Multiple vCenter or ESX Hosts – powershell script


If you have more than a few hosts you probably have shared storage, this is what enables a lot of vSphere’s great features like vMotion. For this to work smoothly, however, you need that storage to be available on all your hosts in a consistent way, and this can turn in to quite a challenge as your environment grows.

VCName DatastoreName NfsHost NfsExport
192.168.1.1 NFS1 10.24.1.1 /share
192.168.1.1 NFS2 10.24.1.2 /share
192.168.1.1 NFS3 10.24.1.3 /export
192.168.1.2 NFS1 10.24.1.1 /share
192.168.1.2 NFS2 10.24.1.2 /share
192.168.1.2 NFS3 10.24.1.3 /export
192.168.1.3 NFS1 10.24.1.1 /share
192.168.1.3 NFS2 10.24.1.2 /share
192.168.1.3 NFS3 10.24.1.3 /export
The first thing we should do is define all our NFS mapping information in a CSV file, like this:
One great feature of PowerShell is its Import-Csv cmdlet, which will turn lines from a CSV file into objects that you can access just like any other object. This way, if you’re adding multiple datastores to a single VC, you only have to log in once. Here’s my full solution to the problem, which assumes that you’ve saved your data into a file called nfs.csv.

Import-Csv nfs.csv | Group VCName | Foreach {
Connect-VIServer -Server $_.Name
Foreach ($entry in $_.Group) {
Get-VMHost | New-Datastore -Nfs -Name $entry.DatastoreName `
-Path $entry.NfsExport -NfsHost $entry.NfsHost
}
}
 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × 4 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.