Friday, January 15, 2016

Get vCenter VM folder Path from VMs and Templates- Powercli

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

Recently I was involved with moving/ migrating ESXi server from one vcenter to another vcenter, (Migration is about moving from Windows vCenter 5.5 to vCenter application linux 6.0 based for testing purpose) Everything was smooth, but I found, I lost my Folders and hierarchy in VMs and Template view in new vCenter. To resolve this issue, one has to note down all my folders path for VMs. and replicate the same setup on new vCenter. and then move those VMs to folders respectively. To automate and to retain the same folder settings I wrote this script. It helps me to backup and save the complete folder path of VM in text file and I can later use it to restore or move VMs to those path accurately.
Below is the code for Getting VMFolderPath
 function Get-VMFolderPath {  
 <#  
 .SYNOPSIS  
 Get folder path of Virtual Machines  
 .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-VM vmname | Get-VMFolderPath  
 .EXAMPLE  
  PS> Get-VM | Get-VMFolderPath  
 .EXAMPLE  
  PS> Get-VM | Get-VMFolderPath | Out-File c:\vmfolderPathlistl.txt  
 #>  
  #####################################    
  ## http://kunaludapi.blogspot.com    
  ## Version: 1    
  ## Windows 8.1   
  ## Tested this script on    
  ## 1) Powershell v4    
  ## 2) VMware vSphere PowerCLI 6.0 Release 1 build 2548067    
  ## 3) Vsphere 5.5    
  #####################################    
   Begin {} #Begin  
   Process {  
     foreach ($vm in $Input) {  
       $DataCenter = $vm | Get-Datacenter  
       $DataCenterName = $DataCenter.Name  
       $VMname = $vm.Name  
       $VMParentName = $vm.Folder  
       if ($VMParentName.Name -eq "vm") {  
         $FolderStructure = "{0}\{1}" -f $DataCenterName, $VMname  
         $FolderStructure  
         Continue  
       }#if ($VMParentName.Name -eq "vm")  
       else {  
         $FolderStructure = "{0}\{1}" -f $VMParentName.Name, $VMname  
         $VMParentID = Get-Folder -Id $VMParentName.ParentId  
         do {  
           $ParentFolderName = $VMParentID.Name  
           if ($ParentFolderName -eq "vm") {  
             $FolderStructure = "$DataCenterName\$FolderStructure"  
             $FolderStructure  
             break  
           } #if ($ParentFolderName -eq "vm")  
           $FolderStructure = "$ParentFolderName\$FolderStructure"  
           $VMParentID = Get-Folder -Id $VMParentID.ParentId  
         } #do  
         until ($VMParentName.ParentId -eq $DataCenter.Id) #until  
       } #else ($VMParentName.Name -eq "vm")  
     } #foreach ($vm in $VMList)  
   } #Process  
   End {} #End  
 } #function Get-VMFolderPath    
You can keep this code in your powershell profile. I have given steps editing Profile in this postOnce you setup profile, login into vcenter using powercli, and run below command.

Get-VM | Get-VMFolderPath | Out-File c:\temp\VmFolderPathList.txt
Above is the screenshot of notepad results, if you are not specifying out-file and showing results on console below is the screenshot.
My next blog will be about how to restore or move Vms to their respective folders on another vCenter.

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


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)

Post a Comment