VMware Cloud Community
Deanb61
Contributor
Contributor
Jump to solution

Moving a hard disk with PowerCLI

I have a vm with several disks. I create a new vm, than attach one disk from the first vm to this new one by using New-HardDisk and passing the path to the vmdk.

The final bit I need is to remove the disk from the first vm so that I can power on the second. I can't work out how to do this with powerCLI, although obviously it's fairly easy using the GUI.

Any ideas?

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, Deanb61-

Decent docs:  yes, the VMware support site has the legitimate documentation:  https://www.vmware.com/support/developer/PowerCLI/.  From there you can browse the docs for the desired version of PowerCLI.  Of course, check the latest version for the most up-to-date docs/features/etc.  Paticularly, Get-HardDisk ref is currently at http://pubs.vmware.com/vsphere-55/index.jsp#com.vmware.powercli.cmdletref.doc/Get-HardDisk.html.

You could find the name of the hard disk on the original VM with the first bit below and use that name to then remove said disk.  Or, the second line of code just relies on the datastore path (filename) of the disk, and removes the disk from the given VM.

## get the VM's disks and their names, so as to use its name later
Get-HardDisk -VM myVM | select Name,Filename,CapacityGB

## or, just get the hard disk with the given filename, and remove it
Get-HardDisk -VM myVM | Where-Object {$_.Filename -eq "[myDStore0] myVM/myVM1.vmdk"} | Remove-HardDisk -DeletePermanently:$false

That do it for you?

View solution in original post

Reply
0 Kudos
4 Replies
mattboren
Expert
Expert
Jump to solution

Hello, Deanb61-

You can use the Remove-HardDisk cmdlet to do so.  And, to be sure to only detach the hard disk from the given VM, and not delete the hard disk from the datastore, you can pass $false to the -DeletePermanently switch parameter.  So, for example, to remove "Hard disk 4" from a VM:

Get-VM someOldVM0 | Get-HardDisk -Name "Hard disk 4" | Remove-HardDisk -DeletePermanently:$false

Enjoy.

Deanb61
Contributor
Contributor
Jump to solution

Thanks Matt, that looks very close to the answer. I have a few things to work out, or a few more questions.

First, is there any decent documentation anywhere? e.g. what I thought was the official documentation at VMware.com, the documentation for the Get-HardDisk cmdletdoes not mention the parameter -Name. Am I looking in the wrong place? Also the documentation for the Remove-Harddisk cmdlet does not mention the DeletePermanently parameter.

From what you have given me, I think the only thing I have left is how to identify the disk on the 'source' vm. e.g. I associate the disk on the 'new' vm by the following command.

New-HardDisk -VM $recovervm -DiskPath "[mydatastore]  disks/Machine1.vmdk"

This describes the path to the disk on the source vm that I am moving onto the new machine. I'm trying to fit this into your answer but not having much luck at the moment. e.g. I tried:

Get-VM -Name OldVM | Get-HardDisk -DiskPath  "[mydatastore] disks/Machine1.vmdk" | Remove- -DeletePermanently:$false -Confirm:$false

But this doesn't seem to work. Or is there any way to know that this disk is "hard disk 4" on the original vm? Or can I 'name' this disk when I create it on the original machine?

Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, Deanb61-

Decent docs:  yes, the VMware support site has the legitimate documentation:  https://www.vmware.com/support/developer/PowerCLI/.  From there you can browse the docs for the desired version of PowerCLI.  Of course, check the latest version for the most up-to-date docs/features/etc.  Paticularly, Get-HardDisk ref is currently at http://pubs.vmware.com/vsphere-55/index.jsp#com.vmware.powercli.cmdletref.doc/Get-HardDisk.html.

You could find the name of the hard disk on the original VM with the first bit below and use that name to then remove said disk.  Or, the second line of code just relies on the datastore path (filename) of the disk, and removes the disk from the given VM.

## get the VM's disks and their names, so as to use its name later
Get-HardDisk -VM myVM | select Name,Filename,CapacityGB

## or, just get the hard disk with the given filename, and remove it
Get-HardDisk -VM myVM | Where-Object {$_.Filename -eq "[myDStore0] myVM/myVM1.vmdk"} | Remove-HardDisk -DeletePermanently:$false

That do it for you?

Reply
0 Kudos
Deanb61
Contributor
Contributor
Jump to solution

Thanks for the help, everything now working as I want. Smiley Happy

Reply
0 Kudos