VMware Cloud Community
berndweyand
Expert
Expert
Jump to solution

replace vmdk

Hello,

is there a way to replace the first harddisk with a previously copied new vmdk ?

i have to change the bootdisk in 150 vm. if i remove the disk and add a new one the order of harddisk changes.i only want to change  the underlying vmdk

 

Regards,

 

Bernd

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Afaik, that is not really possible with the cmdlets, but you can use the API method

Something like this for example

$vmName = 'MyVM'
$vmdkName = 'Hard disk 1'
$vmdkFile = '[Datastore] MyVM/NewHD.vmdk'

$vm = Get-View -ViewType VirtualMachine -Filter @{Name=$vmName}
$hd = $vm.Config.Hardware.Device.where{$_.DeviceInfo.Label -eq $vmdkName}[0]

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$oldVMDK = New-Object VMware.Vim.VirtualDeviceConfigSpec
$oldVMDK.Device = $hd
$oldVMDK.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove

$spec.DeviceChange += $oldVMDK

$newVMDK = New-Object VMware.Vim.VirtualDeviceConfigSpec
$newVMDK.Device = $hd
$newVMDK.Device.Backing.FileName = $vmdkFile
$newVMDK.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

$spec.DeviceChange += $newVMDK

$vm.ReconfigVM($spec)


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik, that is not really possible with the cmdlets, but you can use the API method

Something like this for example

$vmName = 'MyVM'
$vmdkName = 'Hard disk 1'
$vmdkFile = '[Datastore] MyVM/NewHD.vmdk'

$vm = Get-View -ViewType VirtualMachine -Filter @{Name=$vmName}
$hd = $vm.Config.Hardware.Device.where{$_.DeviceInfo.Label -eq $vmdkName}[0]

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$oldVMDK = New-Object VMware.Vim.VirtualDeviceConfigSpec
$oldVMDK.Device = $hd
$oldVMDK.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove

$spec.DeviceChange += $oldVMDK

$newVMDK = New-Object VMware.Vim.VirtualDeviceConfigSpec
$newVMDK.Device = $hd
$newVMDK.Device.Backing.FileName = $vmdkFile
$newVMDK.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

$spec.DeviceChange += $newVMDK

$vm.ReconfigVM($spec)


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

Reply
0 Kudos
berndweyand
Expert
Expert
Jump to solution

Thank you LucD,

 

tried this but it always throws me an error:

Exception calling "ReconfigVM" with "1" argument(s): "Invalid configuration for device '1'. Device: Hard disk 1."

tried your example, only modified vmname and vmdkfile

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you have more details (type, format, ...) on the VMDKs?
Are they on a VSAN datastore?
Can you check in the vpxd log if there is more info on why the method failed?

The code works for me with VMDKs on a regular, non-VSAN, VMFS datastore.


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

Reply
0 Kudos
berndweyand
Expert
Expert
Jump to solution

it works now 🙂

seems that there is an misbehaviour of get-view:

-Filter @{Name=$vmName}: $vmname is set to "TestVM", but there are also TestVM1, TestVM2. With this filter get-view retrieves all VM beginning with "TestVM"

Reply
0 Kudos
berndweyand
Expert
Expert
Jump to solution

replaced the line

$vm = Get-View -ViewType VirtualMachine -Filter @{Name=$vmName}

with

$vm = get-vm $vmname | get-view

now i get only the wanted vm.

thank you very much

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Get-View Filter expects a RegEx.
If you want an exact match you can do

 

$vm = Get-View -ViewType VirtualMachine -Filter @{Name="^$($vmName)$"}

 

But my code is working now I assume?

 


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

Reply
0 Kudos
berndweyand
Expert
Expert
Jump to solution

yes - your code does perfectly that what i wanted - thank you

Reply
0 Kudos