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.
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
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
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,
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
The solution provided is not optimal 😞
Thanks LucD
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
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).
$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
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.
One minor correction, the $spec.Config should be outside the foreach loop.
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
I will perform tests with the code, tomorrow I comment the result
Thank you very much LucD 🙂