VMware Cloud Community
sredo26
Contributor
Contributor
Jump to solution

customization clone with powercli

I would like to know if it is possible to create a script with powercli to customize virtual machine's hardware of a clone, as in the following photos:

pastedImage_0.png

pastedImage_4.png

I want to delete independent-persistent disks, is this possible with New-VM command?

Thanks.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Then the only option would be to remove them (temporarily) from the source VM or Template.


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

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Not with the New-VM cmdlet.
But you can manipulate the HW of the clone after the New-VM with Remove-HardDisk, before you power on the new VM.


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

0 Kudos
sredo26
Contributor
Contributor
Jump to solution

The problem is that the virtual machine has 21 hard disks with RDM, total more than 4TB. I don't want to include these disks in the clone and automate it with powercli.

Thanks LucD,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then the only option would be to remove them (temporarily) from the source VM or Template.


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

0 Kudos
sredo26
Contributor
Contributor
Jump to solution

The solution provided is not optimal 😞

Thanks LucD

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could also use the CloneVM method.

That will allow you to exclude harddisks from the cloning process.

But be aware that you will be coding directly against the API method, which might be a bit more complex compared to using a cmdlet.


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is an example script that clones a VM, but excludes the 2nd harddisk.

You can of course change the selection criteria for the to be excluded harddisk(s).

$srcVM = 'VM1'

$tgtVM = 'VM2'

$tgtFolderName = 'TestFolder'

$tgtDatastoreName = 'TestDatastore'

$excludeHD = 'Hard disk 2'


$vm = Get-VM -Name $srcVM

$tgtFolder = Get-Folder -Name $tgtFolderName

$tgtDS = Get-Datastore -Name $tgtDatastoreName


$spec = New-Object VMware.Vim.VirtualMachineCloneSpec


$config = New-Object VMware.Vim.VirtualMachineConfigSpec


$hd = Get-HardDisk -VM $vm -Name $excludeHD


$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec

$devChange.Device = $hd.ExtensionData

$devChange.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove


$config.DeviceChange += $devChange

$spec.Config = $config


$location = New-Object VMware.Vim.VirtualMachineRelocateSpec

$location.Datastore = $tgtDS.ExtensionData.MoRef


$spec.Location = $location

$spec.PowerOn = $false

$spec.Template = $false


$vm.ExtensionData.CloneVM($tgtFolder.ExtensionData.MoRef,$tgtVM ,$spec)


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

0 Kudos
sredo26
Contributor
Contributor
Jump to solution

In this code, independent-persistent disks could be indicated to exclude:

$hd=Get-VM -Name test_02 | Get-HardDisk | Where-Object {($_.Persistence -like "IndependentPersistent")}

foreach ($hds in $hd){

$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec

$devChange.Device = $hds.ExtensionData

$devChange.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove

$config.DeviceChange += $devChange

$spec.Config = $config

}

Thanks LucD.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

One minor correction, the $spec.Config should be outside the foreach loop.

$hd=Get-VM -Name test_02 | Get-HardDisk | Where-Object {($_.Persistence -like "IndependentPersistent")}

foreach ($hds in $hd){

    $devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $devChange.Device = $hds.ExtensionData

    $devChange.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove


    $config.DeviceChange += $devChange

}


$spec.Config = $config


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

0 Kudos
sredo26
Contributor
Contributor
Jump to solution

I will perform tests with the code, tomorrow I comment the result

Thank you very much LucD 🙂

0 Kudos