Highlighted
Enthusiast
Enthusiast

Remove-Harddisk

Jump to solution

Good morning,

I decided to see if I can automate removal og RDMs from a Windows cluster node in order to migrate.

This turned out not to be difficult at all, and the following works:

Get-VM node02 | Get-HardDisk -DiskType RawPhysical, RawVirtual | Remove-HardDisk -Confirm:$false

The problem is that this command also deletes the SCSI controllers.

Is there a way to remove the RDM pointers/disks and still keep the SCSI controllers?

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Enthusiast
Enthusiast

NM, I figured it out.

Since I had riun the first script in this thread, the $hdnames variable was there.

when I after a restart of the PS editor it is no longer there,

It worked when I ran:

$vmName = 'node02'

$vm = Get-VM -Name $vmName

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

Get-HardDisk -VM $vm  -DiskType RawVirtual,RawPhysical -PipelineVariable hd |

ForEach-Object -Process {

    $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $dev.Device = $hd.ExtensionData

    $dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove

    $spec.DeviceChange += $dev

}

$vm.ExtensionData.ReconfigVM($spec)

So, just delete all RDM disks, removing the -Name $hdnames

View solution in original post

0 Kudos
6 Replies
Highlighted
User Moderator
User Moderator

Yes, but you will have to use the API method.

Something like this for example (it removes 3 harddisks while keeping the SCSI controllers to which they are connected)

$vmName = 'Node02'

$hdNames = 'Hard disk 2','Hard disk 3','Hard disk 4'


$vm = Get-VM -Name $vmName

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

Get-HardDisk -VM $vm -Name $hdNames -PipelineVariable hd |

ForEach-Object -Process {

    $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $dev.Device = $hd.ExtensionData

    $dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove


    $spec.DeviceChange += $dev

}


$vm.ExtensionData.ReconfigVM($spec)


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

That will work, thank you.

However, it will require me to fill in the $hdnames, which amounts to a fair bit of work with 36 of htem.

Is there a way to make the $hdnames variable read form the configuration, some working version of:

$hdNames = Get-HardDisk -DiskType RawPhysical, RawVirtual 

this does not work, but you see where I am trying to go with this?

0 Kudos
Highlighted
User Moderator
User Moderator

If you want to remove all RDM disks you could do

$vmName = 'Node02'

$vm = Get-VM -Name $vmName


$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

Get-HardDisk -VM $vm -Name $hdNames -DiskType RawVirtual,RawPhysical -PipelineVariable hd |

ForEach-Object -Process {

    $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $dev.Device = $hd.ExtensionData

    $dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove


    $spec.DeviceChange += $dev

}


$vm.ExtensionData.ReconfigVM($spec)


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

That is what I want 🙂

Thank you, oh Grand Wizard of The PowerCLI 🙂

0 Kudos
Highlighted
Enthusiast
Enthusiast

Worked beautifully until today..

When I run this now, I get:

Get-HardDisk : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At line:9 char:28

+ Get-HardDisk -VM $vm -Name $hdNames -DiskType RawVirtual,RawPhysical  ...

+                            ~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-HardDisk], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetHardDisk

The name is the VM name, and I just copy whatever Get-VM gives me back, but for some reason the name seems to be invalid....?

(I just remember, I patched my vCenter last night, but this should still work?)

0 Kudos
Highlighted
Enthusiast
Enthusiast

NM, I figured it out.

Since I had riun the first script in this thread, the $hdnames variable was there.

when I after a restart of the PS editor it is no longer there,

It worked when I ran:

$vmName = 'node02'

$vm = Get-VM -Name $vmName

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

Get-HardDisk -VM $vm  -DiskType RawVirtual,RawPhysical -PipelineVariable hd |

ForEach-Object -Process {

    $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $dev.Device = $hd.ExtensionData

    $dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove

    $spec.DeviceChange += $dev

}

$vm.ExtensionData.ReconfigVM($spec)

So, just delete all RDM disks, removing the -Name $hdnames

View solution in original post

0 Kudos