In large vSphere environments it can be very tedious to add multiple NFS datastores to lots of hosts.
PowerCLI comes to the rescue as usual.
I needed to add some datastores to all the clustered hosts in a single datacenter, but to skip our non-clustered standalone hosts which are used for backups.
A bit of PowerCLI which should be fairly self-explanatory:
#
# Add Datastores to all hosts in all clusters in a specified datacenter
# If a host isn't in a cluster it won't get the datastore
# Easy enough to change to do all hosts in a datacenter, all in vCenter etc
#
# Change the value below to point it to your own vCenter
$myvCenter = "vcenter.example.com"
# Array of arrays below holds NFS-hostname, NFS-path and Datastore name, should be easy to add to
$nfsArray = @(
@("nfsserver1","/vol/nfspath1","VMstore1"),
@("nfsserver2","/vol/nfspath2","VMstore2"),
@("nfsserver3","/vol/nfspath3","VMstore3")
)
$datacenter = Read-Host -Prompt "Datacenter name"
Write-Host "Datacenter is $datacenter"
connect-viserver -server $myvCenter
#
# Get all hosts in all clusters in the named datacenter
$ObjAllHosts = get-datacenter -name $datacenter | Get-Cluster | Get-VMHost
ForEach($objHost in $ObjAllHosts){
ForEach($nfsItem in $nfsArray) {
Write-Host "Adding datastore" $nfsItem[2] "with path" $nfsItem[0]":"$nfsItem[1] "to" $objHost
New-Datastore -Nfs -NfsHost $nfsItem[0] -Path $nfsItem[1] -Name $nfsItem[2] -VMHost (Get-VMHost $objHost)
}
}
disconnect-viserver -server $myvCenter -Confirm:$false
Great work, I just love it!
Thx for sharing this 😉