PowerCLI

 View Only
  • 1.  Moving Multiple VM to a different Host

    Posted Mar 01, 2021 11:41 AM

    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



  • 2.  RE: Moving Multiple VM to a different Host

    Posted Mar 01, 2021 12:01 PM

    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 


  • 3.  RE: Moving Multiple VM to a different Host

    Posted Mar 01, 2021 12:13 PM

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

     

     



  • 4.  RE: Moving Multiple VM to a different Host

    Posted Mar 01, 2021 12:27 PM

    $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 

     

     



  • 5.  RE: Moving Multiple VM to a different Host

    Posted Mar 01, 2021 12:35 PM

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



  • 6.  RE: Moving Multiple VM to a different Host
    Best Answer

    Posted Mar 01, 2021 01:02 PM

     

    $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 

     


  • 7.  RE: Moving Multiple VM to a different Host

    Posted Mar 02, 2021 08:46 AM

    Hi, did you succeed?



  • 8.  RE: Moving Multiple VM to a different Host

    Posted Mar 05, 2021 12:30 PM

    Hi Mate,

     

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