VMware Cloud Community
J_McCoy
Contributor
Contributor

Clone vmdks to target vm from old vm using csv

So I'm trying to find a way but I'm fairly new to powercli so I figured I'd ask the geniuses here first before deep diving into this myself.

Let's say I have a vm called Donut and and I'm rebuilding it from scratch. We'll call the new vm Donut-rebuild and I have Bobs brother Grif, Simmons and Lopez who all have counter parts with -rebuild on them. I want to clone the vmdk's as secondary drives for easy data migration to their rebuilds while keeping the OS disk on the -rebuild untouched while being able to to do this enmass via csv. 

Is there a good way to do so?

0 Kudos
11 Replies
LucD
Leadership
Leadership

The Copy-Harddisk cmdlet should allow you to perform a copy of a VMDK.
Once the VMDK is copied you can use New-Harddisk with the DiskPath parameter to add this VMDK to a VM.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
J_McCoy
Contributor
Contributor

Could you shoot me an example of how to set this up? Sorry I'm really new to powerCLI

0 Kudos
LucD
Leadership
Leadership

What is there when the code should run?
The original VM and the rebuild VM?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
J_McCoy
Contributor
Contributor

So there will be a csv with a list of VMs and the ones with the -Rebuild name on them in a second column. I need to import that to mass run the cloning so long as it doesn't erase any disks on the -Rebuild vms.

Sorry english isn't my primary x.x

0 Kudos
LucD
Leadership
Leadership

Do I understand correctly that the code also has to do the cloning of the rebuild VM?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
J_McCoy
Contributor
Contributor

I have a separate script for that that builds a bunch of VMs from a template. I just need a way to clone the disk(s) from the old VM to the -Rebuild vm as secondary disks.

0 Kudos
LucD
Leadership
Leadership

And where should those cloned VMDK be stored?
In the folder of the "rebuild" VM?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
J_McCoy
Contributor
Contributor

Correct.

0 Kudos
J_McCoy
Contributor
Contributor

Any ideas?

0 Kudos
LucD
Leadership
Leadership

Sorry for not working on a Saturday and not meeting your deadline.

Basically, the copy and attachment of the VMDK are quite straightforward.
Something like this

$sourceVMName = 'SourceVM'
$targetVMName = 'TargetVM'

$sourceVM = Get-VM -Name $sourceVMName
$targetVM = Get-VM -Name $targetVMName
$sourceHD = Get-HardDisk -VM $sourceVM -Name 'Hard disk 1'

$vmdk = Copy-HardDisk -HardDisk $sourceHD -DestinationPath (Split-Path -Path $targetVM.ExtensionData.Summary.Config.VmPathName -Parent)
New-HardDisk -VM $targetVM -DiskPath $vmdk.Filename | Out-Null

You can do this in a foreach loop for each entry in for example a CSV.



Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
J_McCoy
Contributor
Contributor

Thank you so much! 

I hadn't realized it was Saturday....sorry.

The days have blurred together.

0 Kudos