Skip to content

Instantly share code, notes, and snippets.

@JamesCalleja
Created June 9, 2016 10:17
Show Gist options
  • Select an option

  • Save JamesCalleja/454e24018c73afd85357b66fa55566b0 to your computer and use it in GitHub Desktop.

Select an option

Save JamesCalleja/454e24018c73afd85357b66fa55566b0 to your computer and use it in GitHub Desktop.
##########################################################################################
#Written By James Calleja #
#20/05/2016 #
# #
#this scrip will migrate a host from one vsphere to another or from cluster to cluster in#
#the same vSphere #
##########################################################################################
$nl = [Environment]::NewLine
#get creds for the operation & the source Vcentre
$vcCreds = Get-Credential -Message 'Vcentre Credentials' -UserName 'headoffice\'
$HostCreds = Get-Credential -Message 'Host root credentials' -UserName 'root'
$sourceVC = Read-Host 'Enter the Source Vsphere server'
connect-viserver $sourcevc -credential $vcCreds
#list hosts and select the targets
get-vmhost -server $sourcevc | select name,state -Unique | fl
$search = read-host 'Enter search criteria for the hosts you want to move'
$search = '*' + $search + '*'
#get the target vcentre server and display clusters and folders
$targetvc = Read-Host 'enter the target vsphere server'
$nl
$hosts
$nl
connect-viserver $targetvc -credential $vcCreds
$nl
write-host 'Clusters:'
Get-Cluster -server $targetvc | select name -Unique | fl
write-host 'Folders:'
Get-Folder -server $targetvc | select name -Unique | fl
$dest = read-host 'Enter the target Cluster name'
#Move the hosts over and connect them in the destination
$hosts = Get-VMHost -Server $sourcevc| ?{$_.name -like $search}
$hosts | Set-VMHost -State Disconnected
$hosts | %{add-vmhost -name $_.name -server $targetvc -location $dest -Credential $Hostcreds -force}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment