PowerCLI – Clone VM

Shares

I had to figure out how to make sure that VM with vCenter server is protected against software failure. I could use vCenter server hearth beat but is expensive and I don’t need super hiper HA for my vCenter server. I decided to use PowerCLI to make a clone of vCenter server virtual machine and keep two copies. Thanks to my colleague Grzesiek from http://psvmware.wordpress.com who helped me out with a script.

Add-PSSnapin VMware.VimAutomation.Core
#variables
$VC='vcenter-server-FQDN-or-IP'

#import credentials
$pwd = Get-Content D:\tools\scripts\ap-vcs-credentials | ConvertTo-SecureString
$credentials = New-Object System.Management.Automation.PsCredential “username“, $pwd
#Connect to vCenter
Connect-VIServer -Server $VC
$sourceVM = 'test01'

#in $respool you can specify ESX host, cluster or resource pool
$respool=Get-VMhost -Name "ESXi host name"
$datastore=Get-datastore -Name 'datastore name'
$cloneName = $sourceVM+'-01'
#Remove second copy of VM
Remove-VM $sourceVM'-02' -Confirm:$false -DeletePermanently:$true
#Rename latest VM copy
Get-VM -Name $sourceVM'-01' | Set-VM -Name $sourceVM'-02' -confirm:$false
#Clone VM
if(New-VM -Name $cloneName -VM $sourceVM -ResourcePool $respool -Datastore $datastore -DiskStorageFormat Thin ){"DONE"}else{"Something wrong with cloning"}
Disconnect-VIServer -Confirm:$false

 

0 0 votes
Article Rating

Artur Krzywdzinski

Artur is Consulting Architect at Nutanix. He has been using, designing and deploying VMware based solutions since 2005 and Microsoft since 2012. He specialize in designing and implementing private and hybrid cloud solution based on VMware and Microsoft software stacks, datacenter migrations and transformation, disaster avoidance. Artur holds VMware Certified Design Expert certification (VCDX #077).

You may also like...

Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Grzegorz Kulikowski

dirty scripting 😉 heheheh

Paul Gienger

You may want to check the disk format after the clone operation. In a test I am running this command to duplcate a VM:
PowerCLI C:> new-vm -vmhost esxi3-9 -Datastore nfs_tpl_1 -DiskStorageFormat Thin -VM 2008r2srvrDCtemplate -name 2008r2DC-200

The resulting VM has Thick-eager disks according to the web client. -DiskStorageFormat takes an object that I presume needs to be properly created and passed. I haven’t figured that part out yet.

[…] Cloning vCenter for backup purposes is fully supported, about one year ago Artur posted a PowerCLI script to automate this […]

4
0
Would love your thoughts, please comment.x
()
x

FOR FREE. Download Nutanix port diagrams

Join our mailing list to receive an email with instructions on how to download 19 port diagrams in MS Visio format.

NOTE: if you do not get an email within 1h, check your SPAM filters

You have Successfully Subscribed!

Pin It on Pinterest