PowerCLI script to move a virtual disk between two VMs

<#
    MoveVD.ps1
    Move a virtual disk between two VMs

    Recently I was requested for writing a script to easily detach a virtual disk from a VM and attach it to another VM.
	

    .History.
	2020/05/28 - 0.1 - Reza Rafiee		- Initial version
	

#>

###############################
Write-host (" ")
$SourceVM = vRead-Host "Enter Source VM Name "
$srcVM=Get-VM -Name $SourceVM

Write-host ("The attached virtual disks on $srcVM.name ")
get-vm -name $srcVM | Get-HardDisk | Select Name,CapacityGB,Persistence,Filename

Write-host (" ")

$VDiskNumber =	Read-Host "Enter the Virtual Hard Disk Number that you want to detach from $srcVM.name  "

$VDiskSize = Read-Host "Enter the Disk Size (GB) "

Write-host (" ")

$TargetVM = Read-Host "Enter Target VM Name "




$trgVM= Get-VM -Name $TargetVM
$trgDisk="Hard Disk $VDiskNumber"

$disk=get-vm -name $srcVM | Get-HardDisk | Where-Object {($_.Name -eq $trgDisk) -AND ($_.CapacityGB -eq $VDiskSize)}


If ($disk -eq $null){
write-host ("No Hard Disk found as ($trgDisk - $VDiskSize GB) on $SourceVM")
exit
}

$confirmation = Read-Host -Prompt "Are you sure you want to detach ($trgDisk - $VDiskSize GB) on $SourceVM and attach it to $TargetVM ? [y/n]"

If ($confirmation -eq "y") {
	Remove-HardDisk $disk -Confirm:$false
	New-HardDisk -VM $trgVM -DiskPath $disk.Filename
#You can also specify the SCSI controller of which the disk should be attached to by adding the following parameter to the above command:  -Controller "SCSI Controller 0"
	Write-host (" ")
	Write-host ("The attached virtual disks on $trgVM.name ")
	get-vm -name $trgVM | Get-HardDisk | Select Name,CapacityGB,Persistence,Filename
	
	}
###############################

Connect vNIC on a VM to network using command line

Once upon a time I had an ESXi host in disconnected state and the management services were out of order and even restarting management services couldn’t help out to get it back to manageable state.

While the Host was partially manageable we had to connect a network interface of a VM to network but the only option was command line. The below commands did the job:

You will need to find the VM ID and the vNIC device ID as well using the below two commands:

vim-cmd vmsvc/getallvms | grep "VM_Name"
vim-cmd vmsvc/get.configuration "VM_ID"

Then you can run the below command to connect/disconnect the vNIC:

vim-cmd vmsvc/device.connection true|false

example:

Some useful tips for powering off VMs from ESXi Command Line

There might be occasions that you need to power off VMs from ESXi command line for some reason. knowing how to do it can save you time and effort from dealing with corrupted virtual disks and VMs. For instance an ESXi host loses all network connectivity due to a driver and firmware compatibility issue and you have to reboot the host but the VMs are still running with no network connectivity. In that case it will be safer if you gracefully shutdown the VMs and then put the host in maintenance mode and then reboot the ESXi host. Let’s see how to do it:

First of all you will need to grab Virtual Machines ID by running the below command:

vim-cmd vmsvc/getallvms

Below command returns the VM’s power state:

vim-cmd vmsvc/power.getstate VMID

Now you can try grcefully shutdown the VM. If the VM doesn’t respond to graceful shutdown in couple of minutes then you can forcefully power it off by the second command:

#For gracefull shutdown:
vim-cmd vmsvc/power.shutdown VMID
#For forcefully powering off the VM:
vim-cmd vmsvc/power.off VMID

The alternative way of doing the power off action is using ESXCLI command:

esxcli vms vm list
esxcli vms vm kill --type=[soft,hard,force] --world-id= WorldNumber

If neither of the above work then you will need to kill the VM process using below commands:

#find the VM World ID
esxcli vm process list

#Kill the VM process
esxcli vm process kill --type=[soft,hard,force] --world-id= WorldNumber

Note You can kill VM process using ps and kill commands similar to “esxcli vm process” if esxcli is not functioning.

Once you power off all you VMs then you can put the ESXi host in maintenance mode and power it off or reboot it using the following command:

#put the host in maintenance mode
esxcli system maintenanceMode set --enable true

#Power off the ESXi host
esxcli system shutdown [poweroff,reboot] --delay "seconds" --reason "description"