Automation

 View Only
  • 1.  Remove-Harddisk

    Posted Oct 20, 2020 05:53 AM

    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?



  • 2.  RE: Remove-Harddisk

    Posted Oct 20, 2020 06:15 AM

    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)



  • 3.  RE: Remove-Harddisk

    Posted Oct 20, 2020 07:01 AM

    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?



  • 4.  RE: Remove-Harddisk

    Posted Oct 20, 2020 07:10 AM

    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)



  • 5.  RE: Remove-Harddisk

    Posted Oct 20, 2020 07:37 AM

    That is what I want :)

    Thank you, oh Grand Wizard of The PowerCLI :)



  • 6.  RE: Remove-Harddisk

    Posted Oct 23, 2020 01:17 PM

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



  • 7.  RE: Remove-Harddisk
    Best Answer

    Posted Oct 23, 2020 01:28 PM

    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