'PowerCLI New-HardDisk fails when trying to add an existing hard disk

I am trying to add existing hard disks not used by any VM, but it fails. To simulate this event I've tried to remove the disks from VM then add them back.

$VM = Get-VM -Name $VMname
$disks = $VM | Get-HardDisk 

foreach($disk in $disks){
    Remove-HardDisk $disk -Confirm:$false
}

foreach($disk in $disks){
    New-HardDisk -VM $VM -DiskPath $disk.Filename
}

I'm always getting an error:

New-HardDisk : 01/10/2021 14:54:44  New-HardDisk        Incompatible device backing specified for device '0'.   
At line:5 char:5
+     New-HardDisk -VM $VM -DiskPath $disk.Filename
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-HardDisk], InvalidDeviceBacking
    + FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_AttachHardDisk_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk

How can I fix this ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source