S
S
starsb2018-09-06 11:40:48
PowerShell
starsb, 2018-09-06 11:40:48

How to make a partial backup of a VM using PowerCli?

Greetings.
For a week now I have been struggling with the task - it is necessary to organize a VM backup using PowerCli. No options.
The method chosen is simple - New-VM -VM. We create a clone from the current one.
But there is a problem - part of the VM contains more than 1 disk, they do not need to be copied. Should only be "Hard disk 1". Deleting after creating a clone is also not an option. Too big disks.
In vCenter, when creating a clone in the GUI, in the last step there is "Edit virtual hardware", which allows you to exclude disks from the copy. Here's how to achieve this in PowerCli?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
azarij, 2018-09-06
@starsb

it looks like when they wrote the new-vm cmdlet, they were just too lazy to put additional options for cloning in there ... or there was not much demand for them. however, they are in the API.
The code below works for a vm that has two disks: one system, one left. the disk to be removed is indicated literally.
TK: make a VM clone so that the non-system disk is removed before cloning. all other parameters of the original vm should be copied as is.

$connection = Connect-VIServer -Server 192.168.0.1 -user "domain\user" -Password "[email protected]" # подключаемся к вицентру

$vm = get-vm "original-vm" # засовываем оригинальную ВМ в переменную

$folder = get-folder "folder_name_here" # выставляем в какую папку будет положена копия

$spec = New-Object VMware.Vim.VirtualMachineCloneSpec # создаем объект со спецификацией клона

$spec.Config = New-Object VMware.Vim.VirtualMachineConfigSpec # создаем секцию конфигурации в предыдущем объекте

$excludedisk = New-Object VMware.Vim.VirtualDeviceConfigSpec # создаем объект, указывающий на конфигурацию устройства?

$excludedisk.Operation = "remove" # указываем, что мы хотим сделать с оригинальной конфигурацией

$excludedisk.Device = $vm.ExtensionData.Config.Hardware.device | where diskobjectid -EQ "id diska, kotoryj hotim ubrat"

$spec.Config.DeviceChange += $excludedisk # запихиваем команду на удаление диска в спецификацию для клонирования.

$spec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec # наверно, создаем объект для указания где хранить клон...

$spec.PowerOn = $false # наверно, указываем, что после клонирования клон запускать не надо

$spec.Template = $false #  наверно, указываем, что никакие шаблоны при клонировании не используются

$vm.ExtensionData.CloneVM($folder.ExtensionData.MoRef,"imia_novoj_vm",$spec) # запускаем собственно клонирование с указанной конфигурацией клона

I highly recommend testing this on a test vm before jumping into production with this code.
the idea is honestly stolen from here: https://communities.vmware.com/thread/409695?start...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question