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
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
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
Thanks! That worked out great!