VMware Cloud Community
electricd7
Contributor
Contributor
Jump to solution

How to detach all disks except 0:0 from "machine1" and re-attach all those disks to "machine2"

Hello,

I need to find a way with PowerCLI that I can parse through all the disks on a power-off VM named "machine1" and detach all disks except the system disk (0:0).  Then I want to re-attach those disks to "machine2".  Seems like there should be a way to do this using the Remove-Disks command, but I am not sure how I would keep track of all the disks as machine1 may have a different number of disks attached each time I run this script.  Can anyone help?

CK

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this.

$vmName1 = 'vm1'

$vmName2 = 'vm2'

$vm1 = Get-VM -Name $vmName1

$vm2 = Get-VM -Name $vmName2

$hd = Get-HardDisk -VM $vm1

$cntrl = Get-ScsiController -VM $vm1

$cntrl0 = $cntrl | where{$_.ExtensionData.BusNumber -eq 0}

$hd | %{

  if( $_.ExtensionData.ControllerKey -ne $cntrl0.ExtensionData.Key -or

      $_.ExtensionData.UnitNumber -ne 0){

    $vmdkFile = $_.Filename

    Remove-HardDisk -HardDisk $_ -Confirm:$false

    New-HardDisk -VM $vm2 -DiskPath $vmdkFile -Confirm:$false

  }

}

Note that the script doesn't take into account the original SCSI controller, it just adds the VMDK to the first SCSI controller


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this.

$vmName1 = 'vm1'

$vmName2 = 'vm2'

$vm1 = Get-VM -Name $vmName1

$vm2 = Get-VM -Name $vmName2

$hd = Get-HardDisk -VM $vm1

$cntrl = Get-ScsiController -VM $vm1

$cntrl0 = $cntrl | where{$_.ExtensionData.BusNumber -eq 0}

$hd | %{

  if( $_.ExtensionData.ControllerKey -ne $cntrl0.ExtensionData.Key -or

      $_.ExtensionData.UnitNumber -ne 0){

    $vmdkFile = $_.Filename

    Remove-HardDisk -HardDisk $_ -Confirm:$false

    New-HardDisk -VM $vm2 -DiskPath $vmdkFile -Confirm:$false

  }

}

Note that the script doesn't take into account the original SCSI controller, it just adds the VMDK to the first SCSI controller


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

0 Kudos
electricd7
Contributor
Contributor
Jump to solution

Thanks!  That worked out great!

0 Kudos