VMware Cloud Community
mrray
Enthusiast
Enthusiast
Jump to solution

Remove-Harddisk

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?

Reply
0 Kudos
1 Solution

Accepted Solutions
mrray
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
mrray
Enthusiast
Enthusiast
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
mrray
Enthusiast
Enthusiast
Jump to solution

That is what I want 🙂

Thank you, oh Grand Wizard of The PowerCLI 🙂

Reply
0 Kudos
mrray
Enthusiast
Enthusiast
Jump to solution

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?)

Reply
0 Kudos
mrray
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos