Hi All Experts :),
I did use the below code for migrating the VM from one host to another and its working fine. But I am looking to migrate some multiple VMs and want the script to read the VM name and Host name from a CSV file or path. Please could you let me know what code I have to use in this below script.
$vms = Get-VM -Name VM-Name
$desthost = Get-VMHost -Name "esxhost.local"
foreach ($singlevm in $vms) {
Shutdown-VMGuest -VM $singlevm -Confirm:$false
while ((Get-VM $singlevm).PowerState -ne "PoweredOff") { Start-Sleep -Seconds 5 }
Move-VM -vm $singlevm -Destination $desthost
Start-VM -VM $singlevm
}
Moderator edit by wila: Moved to PowerCLI discussions
Bye
Fabio
Ciao
You can insert these command
$vms = import-csv d:\temp\servers.csv | select -ExpandProperty name
Where a column of the csv file has the header name.
If you want export the VM name with Status power ON from vcenter:
Hi Fabio,
I am only looking to migrate the VM to another host. The code which I pasted below is working fine but the thing is that I need to put the VM Name and Host name in this scripts file. What I want that the code should read a csv file and get the VM name and Host name from there to move. So here which code I need to add and where? Also what input I need to add in csv file. Please help me with full code which include the one I pasted. Sorry I am not good in scripting hence asking this much :).
$vms = import-csv d:\temp\servers.csv | select -ExpandProperty name
$desthost = Get-VMHost -Name "esxhost.local"
foreach ($singlevm in $vms) {
Shutdown-VMGuest -VM $singlevm -Confirm:$false
while ((Get-VM $singlevm).PowerState -ne "PoweredOff") { Start-Sleep -Seconds 5 }
Move-VM -vm $singlevm -Destination $desthost
Start-VM -VM $singlevm
}
and the csv file must have a single column with the name of the VMs (the name you see from the vcenter inventory and not the hostname)
and lastly do a test first with only one test VM
Bye Fabio
Hi Fabio,
Thanks your input is working greatly for the VMs , one more thing is that I need the Esxi host to be read from a csv file along with VM name on which VM will migrated . could you please help with that too :).
This is code for esxi host $desthost = Get-VMHost -Name "esxhost.local".
Bye
Fabio
Hi, did you succeed?
Hi Mate,
Yes its working as expected. Thank you very much for helping out me with this script mate. Kudos to you!!!