Automation

 View Only
  • 1.  customization clone with powercli

    Posted Feb 18, 2020 10:22 AM

    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:

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

    Thanks.



  • 2.  RE: customization clone with powercli

    Posted Feb 18, 2020 10:44 AM

    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.



  • 3.  RE: customization clone with powercli

    Posted Feb 18, 2020 11:06 AM

    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,



  • 4.  RE: customization clone with powercli
    Best Answer

    Posted Feb 18, 2020 11:14 AM

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



  • 5.  RE: customization clone with powercli

    Posted Feb 18, 2020 12:26 PM

    The solution provided is not optimal :-(

    Thanks LucD



  • 6.  RE: customization clone with powercli

    Posted Feb 18, 2020 01:04 PM

    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.



  • 7.  RE: customization clone with powercli

    Posted Feb 18, 2020 01:41 PM

    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)



  • 8.  RE: customization clone with powercli

    Posted Feb 18, 2020 02:17 PM

    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.



  • 9.  RE: customization clone with powercli

    Posted Feb 18, 2020 02:33 PM

    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



  • 10.  RE: customization clone with powercli

    Posted Feb 18, 2020 02:38 PM

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

    Thank you very much LucD :-)