Friday, January 15, 2016

Move/Migrate VMs to folder Path on another vCenter - Powercli

This is my second post in the 2016, first post was about Getting VMs folder Path from VMs and Templates vCenter., Which helped me to backup those VMs folder path from VMs and template, but the next task was to move vms to its respective folder.

This is the script I used to migrate VMs to folder path, You can check on how to keep this function in Powershell profile from my earlier post. Also make sure you have already got vms folder path before moving esxi to new vcenter and list is in text file as shown earlier.
 function Move-VMtoFolderPath {  
 <#  
 .SYNOPSIS  
 Move VM to folder path  
 .DESCRIPTION  
 The function retrives complete folder Path from vcenter (Inventory >> Vms and Templates)  
 .NOTES   
 Author: Kunal Udapi  
 http://kunaludapi.blogspot.com  
 .PARAMETER N/a  
 No Parameters Required  
 .EXAMPLE  
  PS> Get-Content -Path c:\temp\VmFolderPathList.txt | Move-VMtoFolderPath  
 #>  
  #####################################    
  ## http://kunaludapi.blogspot.com    
  ## Version: 1    
  ##    
  ## Tested this script on    
  ## 1) Powershell v4    
  ## 2) VMware vSphere PowerCLI 6.0 Release 1 build 2548067    
  ## 3) Vsphere 5.5    
  #####################################    
   Foreach ($FolderPath in $Input) {  
     $list = $FolderPath -split "\\"  
     $VMName = $list[-1]  
     $count = $list.count - 2  
     0..$count | ForEach-Object {  
          $number = $_  
       if ($_ -eq 0 -and $count -gt 2) {  
               $Datacenter = Get-Datacenter $list[0]  
          } #if ($_ -eq 0)  
       elseif ($_ -eq 0 -and $count -eq 0) {  
               $Datacenter = Get-Datacenter $list[$_]  
               #VM already in Datacenter no need to move  
         Continue  
       } #elseif ($_ -eq 0 -and $count -eq 0)  
       elseif ($_ -eq 0 -and $count -eq 1) {  
         $Datacenter = Get-Datacenter $list[$_]  
       } #elseif ($_ -eq 0 -and $count -eq 1)  
       elseif ($_ -eq 0 -and $count -eq 2) {  
         $Datacenter = Get-Datacenter $list[$_]  
       } #elseif ($_ -eq 0 -and $count -eq 2)  
          elseif ($_ -eq 1) {  
               $Folder = $Datacenter | Get-folder $list[$_]  
          } #elseif ($_ -eq 1)  
          else {  
         $Folder = $Folder | Get-Folder $list[$_]  
          } #else  
     } #0..$count | foreach  
     Move-VM -VM $VMName -Destination $Folder  
   } #Foreach ($FolderPath in $VMFolderPathList)  
 }#function Set-FolderPath  

Next is login to new vcenter using powercli and execute. (Make sure you have datacenter, and folder names are same on other vCenter, and created them time ahead, so you dont miss the thing and retain all the folder structure as it is)
Get-Content -Path c:\temp\VmFolderPathList.txt | Move-VMtoFolderPath

Once executed, you will see what vms has been moved.
You can also monitor tasks in vCenter to view what is going on. 
once everything is fine, you will see actual result in VMs and template under Inventory, all the vms moved to its respective folder.

Other Useful blogs

Exporting virtual machine annotation (Attributes) and notes to CSV file - Powercli

Move/Migrate VMs to folder Path on another vCenter - Powercli

Get vCenter VM folder Path from VMs and Templates- Powercli

Importing VM annotation (Attributes) and notes from CSV file into vCenter - Powercli

Import vCenter roles (privileges) - Powercli

Export vcenter roles (privileges)

2 comments:

Unknown said...

Warns that move-vm is being deprecated, followed by failed with the following message "The request refers to an unexpected or unknown type."
Thanks in advance!

Anonymous said...

Same message here.