VMware Cloud Community
jparnell
Hot Shot
Hot Shot
Jump to solution

Remove hard disk whilst virtual machine is on

I have a requirement to script the removal of a hard disk from a virtual machine whilst it is powered on. I can do this OK from the virtual infrastructure client but if i try and do it from the PowerCLI I receive an error that the operation cannot be performed whilst the vm is on. Any ideas on how to get around this? Here are the commands im using:

$hd = get-harddisk -vm myvmname | where {$_.Name -eq "Hard Disk Name"}

remove-harddisk -harddisk $hd -Confirm:$false

I'm running PowerCLI 4.0U1 and the latest version of vSphere ESXi and vCenter.

Thanks,

James

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can remove hard disks from a powered on guest when the disks are connected to a SCSI controller.

For IDE hard disks this won't work.

And you're right, the Remove-Harddisk cmdlet doesn't seem to take that into account.

It will only remove hard disks when the guest is powered off.

But you can use the SDK method ReconfigVM_Task method.

$vmName = "MyVM"
$hdName = "Hard disk 3"

$vm = Get-VM $vmName | Get-View
$tgtdev = $vm.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $hdName}
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = "remove"
$dev.device = $tgtdev
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.DeviceChange += $dev
$vm.ReconfigVM($spec)

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You can remove hard disks from a powered on guest when the disks are connected to a SCSI controller.

For IDE hard disks this won't work.

And you're right, the Remove-Harddisk cmdlet doesn't seem to take that into account.

It will only remove hard disks when the guest is powered off.

But you can use the SDK method ReconfigVM_Task method.

$vmName = "MyVM"
$hdName = "Hard disk 3"

$vm = Get-VM $vmName | Get-View
$tgtdev = $vm.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $hdName}
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = "remove"
$dev.device = $tgtdev
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.DeviceChange += $dev
$vm.ReconfigVM($spec)

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
jparnell
Hot Shot
Hot Shot
Jump to solution

Thank you so much. Works brilliantly!

0 Kudos