View Only
  • 1.  replace vmdk

    Posted Sep 13, 2022 09:04 AM


    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





  • 2.  RE: replace vmdk
    Best Answer

    Posted Sep 13, 2022 09:31 AM

    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

  • 3.  RE: replace vmdk

    Posted Sep 13, 2022 12:13 PM

    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

  • 4.  RE: replace vmdk

    Posted Sep 13, 2022 12:47 PM

    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.

  • 5.  RE: replace vmdk

    Posted Sep 13, 2022 01:30 PM

    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"

  • 6.  RE: replace vmdk

    Posted Sep 13, 2022 01:40 PM

    replaced the line

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


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

    now i get only the wanted vm.

    thank you very much

  • 7.  RE: replace vmdk

    Posted Sep 13, 2022 04:19 PM

    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?


  • 8.  RE: replace vmdk

    Posted Sep 13, 2022 10:01 PM

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