VMware Cloud Community
SetClear
Enthusiast
Enthusiast

VI ToolKit for Windows - PowerShell: Cloning Hot/Live VM's

Is it possible to clone a running (Live/Hot) VM using the VI ToolKit. I am new to PowerShell and before delving into it I need to know if this is possible.

Or would it be possible to create the clone and then take snapshots and appliy them to the clone?

Which would be a better method of keeping a secondary instance of the VM up to date?

0 Kudos
12 Replies
gary1012
Expert
Expert

Check out pgs. 13 and 20 in this doc. It may help you out.

Automating VMware with PowerShell

Community Supported, Community Rewarded - Please consider marking questions answered and awarding points to the correct post. It helps us all.
SetClear
Enthusiast
Enthusiast

Gary,

Thank you for the useful information, but it does not answer the questions I have.

(We are running the latest versions of ESX V3.5 U2 Version 120512(or something close to that) along with VirtualCenter 2.5 U2.)

Is it possible to clone a running VM from VI ToolKit? (If so what is the syntax)?

Can you apply Snaps from a running VM to a offline clone of itself to keep it in sync using VI ToolKit/ (If so what is the syntax)?

Which would be the more efficient method of keeping a stand by image of an important VM?

0 Kudos
admin
Immortal
Immortal

Not exactly. You have a few options:

1. You can use Lab Manager, refer to this thread.

2. You can use VMware Converter to live clone the VM as if it were a physical system.

3. You can use VCB, which can be automated through PowerShell.

I'm not quite sure what you're trying to do, but each of these would serve a somewhat different need.

SetClear
Enthusiast
Enthusiast

Thanks for the info...

What we are trying to acomplish is to have a clone of an important VM updated automatically (within a resonalble timeframe) so that if/when it goes down hard (BSOD) we can fire up the clone.

This system while updated rather frequently also crashes on a regular basis to the point that it corrupts the O/S beyond repair. This is a highly visible system so we need a robust way to maintain a clone to lets say a 12 hours update.

I can schedule a clone inside Virtualcenter but I am not able to clean up and delete it to create the next update and VirtualCenter wont let you overwrite the Clone if it exists.

The other reason I am looking at VI Toolkit is I can send out notification on the progress (Completed, Successfully, Failed, etc...) which is an important issue that VirtualCenter dose not offer the functionaility.

If VI Toolkit is not able to do this can I use the SDK to create a script to do this? or would this have to be done in C, C# or perl,,,,?

0 Kudos
admin
Immortal
Immortal

The VI Toolkit can clone just the same as VC, the trouble is that you can't clone a running VM. If it's ok to take it down every 12 hours to clone it, you can do it with VI Toolkit. Otherwise I think you should look at VCB.

0 Kudos
Texiwill
Leadership
Leadership

Hello,

Moved to VI Toolkit for Windows forum.


Best regards,

Edward L. Haletky

VMware Communities User Moderator

====

Author of the book 'VMWare ESX Server in the Enterprise: Planning and Securing Virtualization Servers', Copyright 2008 Pearson Education.

SearchVMware Blog: http://itknowledgeexchange.techtarget.com/virtualization-pro/

Blue Gears Blogs - http://www.itworld.com/ and http://www.networkworld.com/community/haletky

As well as the Virtualization Wiki at http://www.astroarch.com/wiki/index.php/Virtualization

--
Edward L. Haletky
vExpert XIV: 2009-2023,
VMTN Community Moderator
vSphere Upgrade Saga: https://www.astroarch.com/blogs
GitHub Repo: https://github.com/Texiwill
0 Kudos
esloof
Expert
Expert

Live Cloning of Virtual Machines - VirtualCenter 2.5 Update 2 provides the ability of creating a clone of a powered-on virtual machine without any downtime to the running virtual machine. Therefore, administrators are no longer required to power off a virtual machine in order to create a clone of it.

[http://ictfreak.files.wordpress.com/2008/07/image33.png]

0 Kudos
SetClear
Enthusiast
Enthusiast

As stated I understand VC 2.5 functionality and know that within the VIC you have the ability to do Live Cloning.

My issue is that I need to automate the process of a clone on a timed bassis.

Now, you can even schedule a clone with the integrated scheduler and thats great but once you create the inital clone and it exists on a VMFS volume the next time it tries to clone it again it will fail as it detects the existing one and says nope, no can do...

So, back to the question at hand...

I need an automated way to create a clone of a live vm.

I need to be able to schedule this on a recurring basis of lets say 12 hours. I do not want to create multiple clones with different names (Only one).

With the above statement said I will then need to be able to delete (or temp rename until the next clone is complete and then delete) and 12 hours later do this process over again.

If the integrated scheduler would allow deletes of VM's I could probably do this from the scheduler but it doesn't. and I really want to encapsulate messaging to give status on the process which VIC does not incorporate.

0 Kudos
SCampbell1
Enthusiast
Enthusiast

Could you just use VMware snapshots rather than clones?

Snap every hour or whatever, and keep as many as you want, deleting the oldest snap.

When you have your "incident" just go back to the previous snapshot, or if necessary, the one before that, depending on how fast the OS corruption takes place.

This wouldn't work if you're worried about ESX failures, but may provide a suitable solution for problems with the VM. There is a disk performance penalty with this, if this is a disk-intensive application.

0 Kudos
Ewan_Curtis
Enthusiast
Enthusiast

You can clone a running VM provided you have VC2.5U2 or higher. I've tested this with U3.

$destdsview = get-view (get-datastore -name "yourdatastore").id #optional if you don't want to move the VM's disk files to a new datastore

$destpoolview = Get-View (Get-Cluster "yourdatacenter" | Get-ResourcePool "Resources" ).ID #this is the root level resource pool

$sourcevmview = Get-View (Get-VM -name "yourVM").id #the VM you want to clone (running or not)

$vmFolder = get-view (get-folder vm).id

$vmclonespec = New-Object VMware.Vim.VirtualMachineCloneSpec

$vmclonespec.location += New-Object VMware.Vim.VirtualMachineRelocateSpec

$vmclonespec.location.datastore = $destdsview.moref #optional if you don't want to move the VM's disk files to a new datastore

$vmclonespec.location.pool = $destpoolview.moref

$vmclonespec.powerOn = $false

$vmclonespec.template = $false

$sourcevmview.CloneVM_Task($vmfolder.moref, "newname", $vmclonespec)

Combine that with the Remove-VM cmdlet and you'll be well on your way to the solution I think you're after.

0 Kudos
admin
Immortal
Immortal

Terrific. I have to sheepishly admit that I didn't know about this new feature. We'll have to get this added to the toolkit but this is a great solution in the meantime.

0 Kudos
SetClear
Enthusiast
Enthusiast

Ewan,

Thanks but I feel the credit for this needs to go to the person that originally posted the code you are using.

I had found this last Thursday and have had it working but have been working on the <code>$VMCloneSpec options to make this function as robust as possible.</code>

<code>The original post of this code can be found at: http://communities.vmware.com/message/907405#907405</code>

<code>This is a great find as it give much needed automation functionality and everyone's input is needed to make this the best product ever. </code>

<code>Not that it isn't already! But there is always room for improvement...</code>

Once I complete what I have I will post it to this thread for everyones use...

Well as stated here is the working code I have...

it works as long as you are not going across hosts/Clusters which is something I have started another discussion on...

*

Add-PSSnapin VMware.VimAutomation.Core

**

Connect-VIServer -Server vc.setclear.com -Protocol https | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

*

$erroractionpreference = "Continue" #"SilentlyContinue"

Function CreateNewClone #Must be passed 5 agruments (<VM to Clone>,<New Clone Name>,<Server[Host/VC]>,<DataStore Name>,<Folder>)

{

$vm2c = $args[0] #Name of the virtual to be cloned.

$destcln = $args[1] #Name of the clone to be made.

$erver = $args[2] #Name of the host or vitrual cener server.

$destdatastr = $args[3] #Name of the destination datastore to which the new clone will be deployed (Optional).

$foldername = $args[4] #Name of the destinaion folder for the clone.

Now "Initializing clone task for: $vm2c >>-->> $destcln" | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

Now "Using DataStore named: $destdatastr" | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

$serv = Connect-VIServer -Server $erver -Protocol https

$FolderID = Get-Folder -Server $serv -Name $foldername

$targetfolder = get-view ($FolderID).ID

$destdsview = get-view (get-datastore -name $destdatastr).id

$VMCloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec

$VMCloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec

$vmclonespec.location.datastore = $destdsview.moref

$VMCloneSpec.powerOn = $false

$vmclonespec.template = $false

$clntask = (Get-View (Get-VM -Name $vm2c).ID).CloneVM_Task($targetfolder.MoRef, $destcln, $VMCloneSpec)

$clntask | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

}

cls

Now "Beginning DR Cloning Process..." | out-file C:\Scripts\SCClone.log -Encoding "ASCII"

if ($args.length -ge 6)

{

$chkvm = $args[0]

$chkcln = $args[1]

$force = $args[5]

Now "Scanning for VM named: $chkvm" | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

If (Get-VM -Name $chkvm)

{ Now "The VM $chkvm was found." | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

Now "Scanning for clone named: $chkcln" | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

If (Get-VM -Name $chkcln)

{ Now "The Clone $chlcln was found." | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

If ($force -eq $true)

{ Now "Removing existing $chkln clone." | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

Remove-VM -VM $chkcln -DeleteFromDisk -Confirm:$false

CreateNewClone $args[0] $args[1] $args[2] $args[3] $args[4]

If ($args[6])

{

(.\blat.exe SCClone.log -t IT-Support@SetClear.com -s "SetClear DR/HA Clone Process report for: $chkvm" -f InfraAlerts@SetClear.com)

}

}

else

{ Now "The Force agrument was not set to true." | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

Now "Script will now exit." | out-file C:\Scripts\SCClone.log -Append

}

}

Else

{ Now "The Clone wasn't found." | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

CreateNewClone $args[0] $args[1] $args[2] $args[3] $args[4]

If ($args[6])

{

(.\blat.exe SCClone.log -t IT-Support@SetClear.com -s "SetClear DR/HA Clone Process report for: $chkvm" -f InfraAlerts@SetClear.com)

}

}

}

Else

{ Now "The Virtual Machine to be cloned wasn't found." | out-file C:\Scripts\SCClone.log -Append -Encoding "ASCII"

Now "In order to use this script you must supply an existing Virtual Machine(State: On/Off) or Template to clone from." | out-file C:\Scripts\SCClone.log -Append

}

}

Else

{ " "

"Incorrect number of argumanets passed."

" "

"Usage of this script is by passing the following argumanets:"

" "

"CloneVM.ps1 <vm2c> <destcln> <server> <destdatastr> <folder> <force>"

" "

" vm2c: Name of the Virtual to be cloned."

" destcln: Name of the Clone to be made."

" server: Name of the host or vitrual cener server."

" destdatastr: Name of the destination datastore(Optional)."

" folder: Name of the destinaion folder for the clone."

" force: $true or $false, If clone already exists. $true = Remove-VM."

" "

"CloneVMc.ps1 SCT SCTc vc.setclear.com 'NYI - Vmware_VR5_1000G-001_ProdClonesOnly' 'Discovered Virtual Machine' $true"

}

*

Remove-PSSnapin VMware.VimAutomation.Core

*

0 Kudos