Since datastore mounts will stay the same, I wanted to copy datastore mount from existing ESX host servers to new ESX servers that I built. I was being lazy and searched for the PowerCLI script. Google took me to ic-freak.nl PowerCLI script that will copy datastore mounts from reference hosts to the new hosts. The problem with the script was that it will only mount the datastore using IP although reference host had DNS based mounts.
vSphere will not think IP mounted and DNS mounted datastores are same even if they point to same datastore and folder. vMotion will fail if you try to vmotion a VM between them. You must either mount all datastores via IP or DNS, not both.
Below is the script without using Get-View statement. Without get-view, scripts are little bit faster using .extensiondata.
$viserver = Read-Host "Enter VI server"
$refhost = Read-Host "Enter Reference host server"
$cluster = Read-Host "Enter Cluster name that has new hosts"
Connect-VIServer $viserver
$REFHOST = Get-VMHost $refhost
$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) {
$NEWHOST | Get-Datastore | Where {$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue
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)" -fore Red
New-Datastore -Nfs -VMHost $NEWHost -Name $Sharename -Path $remotePath -NfsHost $remoteHost | Out-Null
}
}
}
No comments:
Post a Comment