Last active
August 29, 2015 14:05
-
-
Save coza73/461149c412b02cc64851 to your computer and use it in GitHub Desktop.
Scipt to migrate selected templates from one virtualcenter to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
NAME: MigrateTemplages.ps1 | |
AUTHOR: Cory Murdoch | |
EMAIL: cory at coryandwendy dot com | |
BLOG: http://vspherepowershellscripts.blogspot.com | |
.SYNOPSIS | |
Scipt to migrate selected templates from one virtualcenter to another | |
.DESCRIPTION | |
Exports templates from a selected folder location and importes them into selected | |
cluster, datastore/datastorecluster and network of a selected virtualcenter | |
This script requires ovftool available from vmware.com | |
.EXAMPLE | |
Copy-Templates -SourceViServer vcenter1 -DestinationViServer vcenter2 -DestinationDataStore datastore2 \ | |
-SourceDataCenter datacenter1 -DestinationDataCenter datacenter2 -DestinationCluster "cluster02 (Dev)" \ | |
-SourceNetworkName Prod -DestinationNetworkName Dev \ | |
-SourceFolderName templates -DestinationFolderName templates -TemplateTempFolder e:\templates\ | |
.PARAMETER SourceViServer | |
Source virtalcenter | |
.PARAMETER DestinationViServer | |
Destination virtualcenter | |
.PARAMETER DestinationDataStore | |
Destination datastore or datastore cluster name | |
.PARAMETER SourceDataCenter | |
Datacenter of source virtualcenter that contaings the templates | |
.PARAMETER DestinationDataCenter | |
Datacenter of destination virtualcenter that will contain the templates | |
.PARAMETER DestinationCluster | |
Cluster in which the templates will reside | |
.PARAMETER SourceNetworkName | |
The network name of the network port group that the source template is connected to | |
.PARAMETER DestinationNetworkName | |
The network name of the network port group that the destination template will be connected to | |
.PARAMETER SourceFolderName | |
The folder the Templates reside. This script will not recurse folders | |
.PARAMETER DestinationFolderName | |
The folder you wish the templates to be placed on the destination | |
.PARAMETER TemplateTempFolder | |
The folder to store the exported template ova's on the system that is running the script | |
#> | |
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ) | |
{ | |
Add-PsSnapin VMware.VimAutomation.Core | |
Add-PSSnapin "Vmware.VimAutomation.Core" | |
} | |
function log ([string]$logdata){ | |
$logfile = "c:\temp\MigrateTemplate.log" | |
$date = get-date | |
Write-Output "$date - $logdata" | Out-File $logfile -width 240 -Append | |
} | |
Log "Starting" | |
# check first if ovf tool is installed if not exit | |
$ovftoolpaths = ("C:\Program Files (x86)\VMware\VMware OVF Tool\ovftool.exe", "C:\Program Files\VMware\VMware OVF Tool\ovftool.exe") | |
$ovftool = '' | |
foreach ($ovftoolpath in $ovftoolpaths) | |
{ | |
if(test-path $ovftoolpath) | |
{ | |
$ovftool = $ovftoolpath | |
} | |
} | |
if (!$ovftool) | |
{ | |
write-host -ForegroundColor red "ERROR: OVFtool not found in it's standard path." | |
Log "ERROR: OVFtool not found in it's standard path." | |
write-host -ForegroundColor red "Edit the path variable or download ovftool here: http://www.vmware.com/support/..." | |
exit | |
} | |
function Export-VM { | |
param | |
( | |
[parameter(Mandatory=$true)][array] $vm, | |
[parameter(Mandatory=$true)][String] $destination | |
) | |
$vmd = $($vm.name) | |
$moref = $vm.extensiondata.moref.value | |
$session = Get-View -Id SessionManager | |
$ticket = $session.AcquireCloneTicket() | |
& $ovftool "--I:sourceSessionTicket=$($ticket)" "vi://$($defaultviserver.name)?moref=vim.VirtualMachine:$($moref)" "$($destination)$($vmd.substring(0,$vmd.Length)).ova" | |
write-host ' ' | |
} | |
function Copy-Templates { | |
param | |
( | |
[parameter(Mandatory=$true)][String] $SourceViServer, | |
[parameter(Mandatory=$true)][String] $DestinationViServer, | |
[parameter(Mandatory=$true)][String] $DestinationDataStore, | |
[parameter(Mandatory=$true)][String] $SourceDataCenter, | |
[parameter(Mandatory=$true)][String] $DestinationDataCenter, | |
[parameter(Mandatory=$true)][String] $DestinationCluster, | |
[parameter(Mandatory=$true)][String] $SourceNetworkName, | |
[parameter(Mandatory=$true)][String] $DestinationNetworkName, | |
[parameter(Mandatory=$true)][String] $SourceFolderName, | |
[parameter(Mandatory=$true)][String] $DestinationFolderName, | |
[parameter(Mandatory=$true)][String] $TemplateTempFolder # Always follow with a backslash | |
) | |
write-host "Connecting .... > to $SourceViServer" | |
connect-viserver $SourceViServer | |
write-host 'Creating list of Templates -->' -foregroundcolor blue | |
$templatesObject = get-Datacenter $SourceDataCenter | get-folder $SourceFolderName| get-template -NoRecursion | |
write-host 'Reading from List of Templates -->' -foregroundcolor blue | |
foreach($template in $templatesObject.name){ | |
connect-viserver $SourceViServer | |
#Export the selected template to local storage | |
$templateObject = get-template -Server $SourceViServer $template | |
. Export-VM -vm $templateObject -destination $TemplateTempFolder | |
write-host "Disconnecting .... > " $SourceViServer | |
disconnect-viserver $SourceViServer -force -confirm:$false -ErrorAction:SilentlyContinue | |
write-host "Connecting .... > " $DestinationViServer | |
connect-viserver $DestinationViServer | |
$vmhost = Get-Cluster $DestinationCluster | Get-VMHost | |
$DestinationHost = $vmhost[0] | |
#Delete the matching template from Destination if exists | |
if (!(Get-Template $template -ErrorAction:SilentlyContinue)){ | |
Write-Host 'Destination template does not exist --> '"$template" | |
} | |
Else{ | |
write-host 'Deleting old template --> ' "$template" | |
Remove-Template -DeletePermanently -Template $template -confirm:$false -server $DestinationViServer | |
} | |
write-host 'Deploying OVA --> ' "$template" -foregroundcolor blue | |
$source = $($TemplateTempFolder + $template + ".ova") | |
write-host $source | |
$session = Get-View -Server $DestinationViServer -Id SessionManager | |
$ticket = $session.AcquireCloneTicket() | |
& $ovftool "--noSSLVerify" "--diskMode=thin" "--I:targetSessionTicket=$ticket" "-ds=$DestinationDataStore" "--net:$SourceNetworkName=$DestinationNetworkName" "$source" "vi://$($DestinationViServer)/$($DestinationDataCenter)?dns=$DestinationHost" | |
write-host ' ' | |
write-host 'Deleting OVA from $TemplateTempFolder --> ' $template -foregroundcolor blue | |
del $($TemplateTempFolder + $template + ".ova") | |
write-host 'Converting VM to Template --> ' $template -foregroundcolor blue | |
set-vm -vm $template -Server $DestinationViServer -ToTemplate -name "$template" -confirm:$false | |
Move-Template -Template $template -destination (Get-datacenter $DestinationDataCenter | get-folder $DestinationFolderName) | |
write-host "Disconnecting .... > " $DestinationViServer | |
disconnect-viserver $DestinationViServer -force -confirm:$false -ErrorAction:SilentlyContinue | |
} | |
write-host 'Disconnecting .... > server :' $SourceViServer | |
disconnect-viserver $SourceViServer -Force -confirm:$false -ErrorAction:SilentlyContinue | |
write-host 'Disconnecting .... > server :' $DestinationViServer | |
disconnect-viserver $DestinationViServer -Force -confirm:$false -ErrorAction:SilentlyContinue | |
write-host "Completed" | |
write-host "`n" | |
write-host "The following Templates have been copied:" | |
write-host "From SOURCE ViServer: $SourceViServer" -foregroundcolor green | |
write-host "To DESTINATION ViServer: $DestinationViServer" -foregroundcolor green | |
write-host $templatesObject.Name -foregroundcolor blue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment