Hi PowerCLI gurus,
I am trying to use PowerCLI to automate solution for the KB article Configuring disks to use VMware Paravirtual SCSI (PVSCSI) adapters (1010398).
Remove-HardDisk sometimes gives me a misleading error when I use it to remove a hard disk with a new SCSI controller.
The specified FlatHardDisk 'Hard disk 3' no longer exists or has never existed.
Can someone here explain the error?
My vCenter server version is 5.1.0.
My PowerCLI version is 2.0.
Here's what I do to reproduce the error.
First I connect to vCenter and stop the VM.
$TestVM = "TESTVM"
$TestServer = "vcenter-test.local"
Connect-VIServer -Server $TestServer
Stop-VM -VM $TestVM -Confirm:$false
I add a new hard disk and store it in a variable so I can refer to it for the next steps.
New-HardDisk `
-VM $TestVM `
-Persistence Persistent `
-DiskType Flat `
-CapacityKB 1024 `
-StorageFormat Thin `
-OutVariable TempDisk
I make a note of its name.
$TempDisk.Name
Because my VM already has 2 disks, the output is "Hard disk 3".
Next I add a new SCSI controller to the hard disk.
New-ScsiController `
-HardDisk $TempDisk `
-Type ParaVirtual `
-BusSharingMode NoSharing
Now I try to remove the hard disk by passing the variable to Remove-Disk.
Remove-HardDisk -HardDisk $TempDisk -DeletePermanently -Confirm:$false
It fails with an error that suggests "Hard disk 3" has already been removed.
Remove-HardDisk : 02/12/2014 18:51:33 Remove-HardDisk The specified FlatHardDisk 'Hard disk 3' no longer exists or has never existed.
At line:1 char:1
+ Remove-HardDisk -HardDisk $TempDisk -DeletePermanently -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Hard disk 3:FlatHardDiskImpl) [Remove-HardDisk], VimException
+ FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_TryValidateDeviceExists_DeviceDoesNotExist,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveHardDisk
But I can see that "Hard disk 3" still exists!
Get-HardDisk -VM $TestVM -Name "Hard disk 3"
CapacityGB Persistence Filename
---------- ----------- --------
0.001 Persistent ...B-DATA-01] TESTVM/TESTVM_9.vmdk
Is this a bug, or am I missing something?
I don't think the cmdlet actually uses the UID, but most probably a MoRef to the vSphere object that represents the Harddisk.
Remember that under the covers most PowerCLI cmdlets make calls to the vSphere API.
The change of the Uid property value shows that the Harddisk connected to the new controller is a new, and different, object.
And that explains why the Remove-Harddisk will not work with the original Harddisk object, it simply doesn't exist anymore.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
As a workaround I can pipe the output of Get-HardDisk into Remove-HardDisk.
Get-HardDisk -VM $TestVM -Name $TempDisk.Name |
Remove-HardDisk -DeletePermanently -Confirm:$false
This time Remove-HardDisk completes successfully.
Now when I try to retrieve "Hard disk 3" with Get-HardDisk I see an error.
HardDisk with name 'Hard disk 3' was not found using the specified filter(s).
The error shows that "Hard Disk 3" is really gone.
Thanks,
Iain
Did you pipe a VirtualMachine object to the Remove-Harddisk cmdlet ?
Or did you use the VM parameter on the cmdlet ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
To produce the error I pass the $TempDisk variable to the HardDisk parameter of Remove-HardDisk.
Remove-HardDisk -HardDisk $TempDisk -DeletePermanently -Confirm:$false
See the first post for the full repro with the context of that command.
The error only happens when I add a new SCSI controller before trying to remove the disk.
The error does not appear if I remove the disk without adding a new SCSI controller.
I suspect the content of $TempDisk doesn't reflect the actual harddisk after you assigned it to a new controller.
That would also explain why it does work when you fetch the Harddisk object after you assign the new controller
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Does Remove-HardDisk refer to the disk by Uid rather than by name?
It turns out that the the Uid of the hard disk changes when you assign it to a new controller.
The Uid in the variable says the disk key is 2002.
$TempDisk.Uid
/VIServer=myuser@vcenter-test.local:443/VirtualMachine=VirtualMachine-vm-346/HardDisk=2002/
But the Uid of the disk called "Hard disk 3" has changed to 2018.
(Get-HardDisk -VM $TestVM -Name $TempDisk.Name).Uid
/VIServer=myuser@vcenter-test.local:443/VirtualMachine=VirtualMachine-vm-346/HardDisk=2018/
I don't think the cmdlet actually uses the UID, but most probably a MoRef to the vSphere object that represents the Harddisk.
Remember that under the covers most PowerCLI cmdlets make calls to the vSphere API.
The change of the Uid property value shows that the Harddisk connected to the new controller is a new, and different, object.
And that explains why the Remove-Harddisk will not work with the original Harddisk object, it simply doesn't exist anymore.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That explains the behavior to my satisfaction.
It's a minor inconvenience, but the workaround is easy enough: refer to the temporary disk by name in the script rather than by object reference.
Thanks for your help, Luc.