CT-FREAK.NL blog had great post on using script to reference a existing host to mount NFS datastores on new ESX host server. I needed to do exactly that so I used the script from the blog to mount NFS datastore. It worked great but the problem was that the script does not distinguish between IP mounted datstore and DNS mounted datastore. My reference host server was using DNS to mount NFS datastore (I don't agree with this method but it was for work) and the script was mounting the datastore using IP on my new host server.
Unfortunately, hosts in the cluster will treat DNS mounted datastore and IP mounted datastore as different datastore even if you are pointing to the same volume on NFS datatstore system. I decided to make some modification to the script to make it bit simpler and mount NFS datastore using exact configuration from the reference host.
The script will mount NFS datastores on hosts in a cluster or sinle host server.
You can download the script on my blog or copy-paste from below. http://powerclinic.blogspot.com/2012/02/mount-nfs-datastore-using-reference.html
################################################################################################################
# VMWare Standard Scripts - PowerCLI
#
# Purpose: Mount NFS datastore on new host server using reference host datstore
# Author: David Chung
# Docs: NA
# Date: 2/4/2012
#
#
# Instruction: Enter VI server name, Reference Host server name
# Modify $hostincluster value either $true or $false
# $false value will only update single ESX host server with NFS mount
#
################################################################################################################
$HostinCluster = $false
$viserver = Read-Host "Enter VI server"
$refhost = Read-Host "Enter Reference host server"
Connect-VIServer $viserver
$REFHOST = Get-VMHost $refhost
If ($HostinCluster) {
$cluster = Read-Host "Enter Cluster that has new host servers"
$NEWHosts = Get-Cluster $cluster | Get-VMHost | sort name
foreach($nfs in (Get-VMhost $REFHOST | Get-Datastore | Where {$_.type -eq "NFS"} )){
[string]$remotePath = $nfs.extensiondata.info.nas.remotepath
[string]$remoteHost = $nfs.extensiondata.info.nas.remotehost
[string]$shareName = $nfs.Name
Foreach ($NEWHost in $NEWHosts) {
If ((Get-VMHost $NEWHOST | Get-Datastore | Where {$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue )-eq $null){
Write-Host "NFS mount $shareName doesn't exist on $($NEWHOST.name)" -fore Red
New-Datastore -Nfs -VMHost $NEWHost.name -Name $Sharename -Path $remotePath -NfsHost $remoteHost | Out-Null
}
}
}
}
Else {
$NEWHOST = Read-Host "Enter new host name"
$Newhost = Get-VMHost $NEWHost
foreach($nfs in ($REFHOST | Get-Datastore | Where {$_.type -eq "NFS"} )){
[string]$remotePath = $nfs.extensiondata.info.nas.remotepath
[string]$remoteHost = $nfs.extensiondata.info.nas.remotehost
[string]$shareName = $nfs.Name
If (($NEWHOST | Get-Datastore | Where {$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue )-eq $null){
Write-Host "NFS mount $shareName doesn't exist on $($NEWHOST.name)" -fore Red
New-Datastore -Nfs -VMHost $NEWHost.name -Name $Sharename -Path $remotePath -NfsHost $remoteHost | Out-Null
}
}
}