Clone Datastores from one ESXi host to Another

As we I progress with our server refresh and I continue to build our global virtualised infrastructure, I am constantly trying to make things easier for my less experienced colleagues and so that there will be consistency in what we do. One of the most tedious tasks must be NFS datastore creation. While we could script this out and create each datastore individually, I figured I would try find a way clone the datastore configuration from one host to another. This way you simply execute the script and let it run along, giving you time for other cool stuff.

This little script will save you hours, especially if you have hundreds of datastores and hosts.

Script 1: This is for host to host clone only

$Source_Host = Read-Host "Enter The Source Host"
$Destination_Host = Read-Host "Enter the Destination Host"
$Source_Root_Password = Read-Host "Enter Root Password for Source Host"
$Destination_Root_Password = Read-Host "Enter Root Password for Destination Host"
Connect-VIServer -Server $Source_Host -User root -Password $Source_Root_Password
Connect-VIServer -Server $Destination_Host -User root -Password $Destination_Root_Password
foreach (
			$datastore in (Get-VMhost $Source_Host | Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"})
		)
		{
			New-Datastore -VMhost $Destination_Host -Nfs -Name $datastore.Name -Path $datastore.RemotePath -NfsHost $datastore.RemoteHost
		}
Disconnect-Viserver * -Confirm:$false

Script 2: This for host to Cluster wide .

$Source_Host = Read-Host "Enter The Source Host"
$Destination_vCenter = Read-Host "Enter the vCenter you want to connect to"
$Cluster = Read-Host "Enter the name of the Cluster to connect to"
$Source_Root_Password = Read-Host "Enter Root Password for Source Host"
Connect-VIServer -server $Source_Host -user root -password $Source_Root_Password 
Connect-VIServer -server $Destination_vCenter
foreach (
			$datastore in (Get-VMhost $Source_Host | Get-Datastore | where {$_.Type -eq "nfs" -and $_.Accessible -eq "true"})
		)
		{ 
			Get-Cluster "$Cluster" | Get-VMhost | New-Datastore -Nfs -Name $datastore.Name -Path $datastore.RemotePath -NfsHost $datastore.RemoteHost
		}
Disconnect-Viserver * -Confirm:$false

Copy the above text in to a text document and save it as a ps1 (powershell) file and run it using PowerCLI.

Leave a Reply

Your email address will not be published.

*
*