The greatest challenge to any thinker is stating the problem in a way that will allow a solution

Bertrand Russell

By

On 25 Feb 2012

PowerCLI, VMware

Tags: ,


Recently I had to migrate a collection of virtual machines from one vCentre to another.  They didn’t have to change datastore, so there was no need to whip VMware Converter out and cause a mass movement of data.  Just a case of unregistering from one vCentre and registering with another.  However, there were over 20 machines, in various datastores hosting some VM’s with duplicate VM names, meaning it would be quite a laborious process, and manual error prone.

So, in order to make the process nice and reliable and avoid any machines getting lost on the way, I wrote a quick script to do the hard work…

This script utilises my stored secure login credentials for the vCentre’s – see my Store Credentials Securely post for further info.

The script relocates all VM’s from with a vApp to folder on the other vCentre.  You can obviously change this as required to suit your requirements.

$SourceVC = "vCenterA"
$SourceVapp = "vAppToMove"
 
$DestVC = "vCenterB"
$DestCluster = "ClusterName"
$DestFolder = "FolderName"
 
$UserFile = "User.fil"
$PassFile = "Pass.fil"
 
# Functions ---------------------------------------------------------------------------------
 
function Log ($text) {
    $stamp = (Get-Date).ToString("HH:mm:ss.fff")
    Write-Host "$stamp | $text"
}
 
# Business part of script -------------------------------------------------------------------
 
Start-Transcript -Path VM-Move-VC.log -Append
 
# Load password credential from encrypted file
try {
    $pass = Get-Content $PassFile -errorAction Stop | ConvertTo-SecureString 
    $user = Get-Content $UserFile -errorAction Stop
    $cred = New-Object System.Management.Automation.PsCredential($user, $pass)
} catch {
    Log "ERROR: Failed to load credentials to use"
    Log $_
    Exit
}
 
# Disconnect any existing VI Server sessions
if ($DefaultVIServers.Count) {
    Log("Disconnect existing vCentre server connections...")
    Disconnect-VIServer -Server * -Force -Confirm:$false
}
 
# Connect to source VC
try {
    Log "Connecting to $SourceVC"
    $VCconn = Connect-VIServer -Server $SourceVC -Credential $cred -errorAction Stop
} catch {
    Log("Unable to connect to vCentre - " + $_)
    Exit
}
 
# Get list of VMs to move
$VMs = Get-VM -Location (Get-vApp $SourceVapp) | Sort
Log "VMXs to reregister..."
$VMs2Move = @()
foreach ($vm in $VMs) {
    $vm2move = "" | Select Name, Path
    $vm2move.Name = $vm.name
    $vm2move.Path = $vm.ExtensionData.Config.Files.VmPathName
    $VMs2Move += $vm2move
    Log ($vm2move.Name + " " + $vm2move.Path)
}
 
# Unregister VMs
foreach ($vm in $VMs) {
    Log ("Unregister " + $vm.Name)
    Remove-VM -VM $vm -DeletePermanently:$false -Confirm:$false
}
 
Disconnect-VIServer -Server $VCconn -Confirm:$false
 
# Connect to destination VC
try {
    Log "Connecting to $DestVC"
    $VCconn = Connect-VIServer -Server $DestVC -Credential $cred -errorAction Stop
} catch {
    Log("Unable to connect to vCentre - " + $_)
    Exit
}
 
# Register VMs
foreach ($vm in $VMs2Move) {
    Log ("Register " + $vm.Name)
    New-VM -VMFilePath $vm.Path -VMHost (Get-Cluster $DestCluster | Get-VMHost | Get-Random) -Location (Get-Folder $DestFolder)
}
 
Disconnect-VIServer -Server $VCconn -Confirm:$false
Stop-Transcript

 


Leave a Reply

XHTML: You can use these tags if you know what they are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.