VMware Cloud Community
system_noida
Enthusiast
Enthusiast
Jump to solution

Moving Multiple VM to a different Host

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

0 Kudos
1 Solution

Accepted Solutions
fabio1975
Commander
Commander
Jump to solution

 

$vms = import-csv c:\tmp\servers.csv 
$vms | foreach {

    Shutdown-VMGuest -VM $_.Name -Confirm:$false
  
    while ((Get-VM $_.Name).PowerState -ne "PoweredOff") { Start-Sleep -Seconds 5 }
  
    Move-VM -vm $_.Name -Destination $_.VMHost
  
    Start-VM -VM $_.Name
  
  }
 
where servers.csv
 
fabio1975_0-1614603613590.png

Bye 

Fabio 

 
Fabio

Visit vmvirtual.blog
If you're satisfied give me a kudos

View solution in original post

0 Kudos
7 Replies
fabio1975
Commander
Commander
Jump to solution

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:

$vcVMname = "vcenter name"
$vcVm = Get-VM -Name $vcVMName
#Export list of poweredOn VM to file for powerOn
$vmservers=Get-VM | Where-Object{$_.powerstate -eq "PoweredOn" -and ($_.Name -ne $vcVMName)} 
$vmservers | select Name | export-csv d:\temp\servers.csv -NoTypeInformation
 
Bye Fabio 
Fabio

Visit vmvirtual.blog
If you're satisfied give me a kudos

0 Kudos
system_noida
Enthusiast
Enthusiast
Jump to solution

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 :).

 

 

0 Kudos
fabio1975
Commander
Commander
Jump to solution

$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)

fabio1975_0-1614601205294.png

and lastly do a test first with only one test VM

 

Bye Fabio 

 

 

Fabio

Visit vmvirtual.blog
If you're satisfied give me a kudos

0 Kudos
system_noida
Enthusiast
Enthusiast
Jump to solution

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".

0 Kudos
fabio1975
Commander
Commander
Jump to solution

 

$vms = import-csv c:\tmp\servers.csv 
$vms | foreach {

    Shutdown-VMGuest -VM $_.Name -Confirm:$false
  
    while ((Get-VM $_.Name).PowerState -ne "PoweredOff") { Start-Sleep -Seconds 5 }
  
    Move-VM -vm $_.Name -Destination $_.VMHost
  
    Start-VM -VM $_.Name
  
  }
 
where servers.csv
 
fabio1975_0-1614603613590.png

Bye 

Fabio 

 
Fabio

Visit vmvirtual.blog
If you're satisfied give me a kudos

0 Kudos
fabio1975
Commander
Commander
Jump to solution

Hi, did you succeed?

Fabio

Visit vmvirtual.blog
If you're satisfied give me a kudos

0 Kudos
system_noida
Enthusiast
Enthusiast
Jump to solution

Hi Mate,

 

Yes its working as expected. Thank you very much for helping out me with this script mate. Kudos to you!!!

0 Kudos