1 2 Previous Next 18 Replies Latest reply on Jan 20, 2016 6:12 AM by virtualinstall

    Export / Import selective Folder structure

    sureshadmin201110141 Enthusiast

      Hi,

       

      I require a script which can do the following,

       

      I have two vCenter VC1 and VC2 and many VM's  with same names are present in both the VC,

       

      1. Export the complete folder structure with VM details of a datacenter from VC1 to a outputfile.

      2. Then in VC2 i need to create only a particular folder with all its its sub-folders from the output file generated from VC1. This folder will be a root folder which is directly under datacenter in VC1.

      3. After creating the particular folder structure, the script should read the vm's that was present in that folder & subfolder from output file and then it should search the same VM's in VC2 and place them into the folders identically.

       

      Example:

       

      The folder1 structure hs been taken from VC1 and has been created in VC2 with all the subfolders and placing the same VM's in the folders.

       

      VC1

        |-----Datacenter1

                    |------------Folder1

                                     |-------SubFolder1

                                                   |----VM1

                                                   |----VM2

                                     |-------Subfolder2

                                                   |-----VM3        

                                                   |-----VM4

                    |-------------Folder2

                                      |--------Subfolder3

                                      |--------Subfolder4

      VC2

        |-----Datacenter1

                    |------------Folder1

                                     |-------SubFolder1

                                                   |----VM1

                                                   |----VM2

                                     |-------Subfolder2

                                                   |-----VM3        

                                                   |-----VM4

       

       

      Thanks in Advance!

        • 1. Re: Export / Import selective Folder structure
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          Not sure if I understood your point 2) but here are 2 scripts that should do what I think you want to do.

           

          Export the VMs and the folderstructure.

           

          New-VIProperty -Name 'BlueFolderPath' -ObjectType 'VirtualMachine' -Value {
              param($vm)
          
              function Get-ParentName{
                  param($object)
          
                  if($object.Folder){
                      $blue = Get-ParentName $object.Folder
                      $name = $object.Folder.Name
                  }
                  elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){
                      $blue = Get-ParentName $object.Parent
                      $name = $object.Parent.Name
                  }
                  elseif($object.ParentFolder){
                      $blue = Get-ParentName $object.ParentFolder
                      $name = $object.ParentFolder.Name
                  }
                  if("vm","Datacenters" -notcontains $name){
                      $blue + "/" + $name
                  }        
          else{             $blue
                  }     }     (
          Get-ParentName $vm).Remove(0,1) } -Force | Out-Null
          $dcName
          = "MyDC"

          Get-VM
          -Location (Get-Datacenter -Name $dcName) | Select Name,BlueFolderPath | Export-Csv "C:\vm-folder.csv" -NoTypeInformation -UseCulture

           

          The script uses a New-VIProperty to fetch the blue folderpath for a VM.

           

          Import the folder structure and move existing VMs.

           

          $newDatacenter = "MyNewDC" 
          $newFolder = "Folder1"

          $startFolder
          = New-Folder -Name $newFolder -Location (Get-Folder -Name vm -Location (Get-Datacenter -Name $newDatacenter)) Import-Csv "C:\vm-folder.csv" -UseCulture | %{     $location = $startFolder
              $_.BlueFolderPath.TrimStart('/').Split('/') | %{         $tgtFolder = Get-Folder -Name $_ -Location $location -ErrorAction SilentlyContinue
                 
          if(!$tgtFolder){             $location = New-Folder -Name $_ -Location $location
                  }        
          else{             $location = $tgtFolder
                  }     }        
          $vm = Get-VM -Name $_.Name -ErrorAction SilentlyContinue
             
          if($vm){         Move-VM -VM $vm -Destination $location -Confirm:$false
              } }

           

          The complete folder structure that was exported will now be imported in datacenter MyNewDC under the folder Folder1.

          • 2. Re: Export / Import selective Folder structure
            alanrenouf Master
            vExpertVMware Employees

            And what my good friend Luc forgot to say is that there is a pre-built advanced function for this in the PowerCLI Book he and others wrote.... PowerCLIBook.Com

             

             

            • 3. Re: Export / Import selective Folder structure
              sureshadmin201110141 Enthusiast

              Thanks so much Luc.

               

              Looks like the export script is VM based. It exports all the VM names and corresponding folders to csv file. But if a folder is empty it does not have a entry in the csv file, so i guess it will not be created in the target VC. I have many folder structures(including subfolders) without VM in it for now, but will be used in future. So is it possible export the CSV based on folders(including empty folders) and corresponding VM's in it. Just not want to miss a single folder in my target vc

              • 4. Re: Export / Import selective Folder structure
                sureshadmin201110141 Enthusiast

                Thanks Alan. That book is a marvellous work! Adopted many of them for my daily work.

                • 5. Re: Export / Import selective Folder structure
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  That is correct, these scripts are VM-based.

                   

                  To export/import also your empty folders, I suggest you use the Export-Folders and Import-Folders functions that come with the book module.

                  • 6. Re: Export / Import selective Folder structure
                    sureshadmin201110141 Enthusiast

                    Luc, i accessed the sybex download page http://spa.sybex.com/WileyCDA/SybexTitle/VMware-vSphere-PowerCLI-Reference-Automating-vSphere-Administration.productCd-0470890797,navId-290600,pageCd-resources.html and downloaded Ch01 sample code.

                     

                    I used Export-folders, Export-vmlocation, Import-folders, Import-vmlocation functions.

                     

                    Anyhow i faced a little problem in Import-vmlocation function. This line given below gave problems,

                     

                     

                    $key =  Split-Path $vmpath.Path | split-path -leaf
                    

                     

                    I altered this line to the one given below and i was able to accomplish the task without any problem.

                     

                     

                    $key =  Split-Path $vmpath.Path -leaf
                    

                     


                    Thanks Luc & Alan.

                    • 7. Re: Export / Import selective Folder structure
                      LucD Guru
                      Community WarriorsvExpertUser Moderators

                      Did that original line produce error messages ?

                      And how did the path look when it gave errors ?

                      • 8. Re: Export / Import selective Folder structure
                        sureshadmin201110141 Enthusiast

                        Luc,

                         

                        Looks like my mistake,

                         

                        I exported the folders and VM using export functions. And i want to import only one folder and subfolders and it's vm's in the target VC's. So i altered the output folder file and created the folders. And while hadling the VM location output file i made a mistake.

                         

                        The original format was like,

                         

                         

                        Name          Path
                        Test01        dc01\folder1\Test01


                        When i alterered the CSV file for selected VM's only, i created a new file with format like

                         

                        Name          Path 
                        Test01        dc01\folder1
                        


                        Sorry for that! Your function works perfect!

                        • 9. Re: Export / Import selective Folder structure
                          CarlosDionizio Novice

                          Hi for all!

                           

                          This post is Old! Any news to migrate folder and structure in to vcenters?

                          I need export folders and vms in  vcenter 5.1 to vcenter 5.5

                           

                          Any software or script do make this??

                          • 10. Re: Export / Import selective Folder structure
                            gtaylor1 Novice

                            Hi LucD,

                             

                            This script works a treat, I'm just wondering if it's possible to import my folder structure to the root of the DC rather than a temp folder (moving them manually won't exactly be difficult, but just wondering if it's possible).

                             

                            How can I also script import of Yellow folders? Not that I have many of them, would be easy enough to remake them manually.

                            • 11. Re: Export / Import selective Folder structure
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

                              If you change the line that sets up the start folder like this, it should import in the root folder.

                               

                              $startFolder = Get-Folder -Name Datacenters

                               

                              • 12. Re: Export / Import selective Folder structure
                                gtaylor1 Novice

                                Hi Luc,

                                 

                                That almost worked, it imported to the root folder but to the root folder of the VC... Not under the datacenter that has been configured. Any advice?

                                • 13. Re: Export / Import selective Folder structure
                                  LucD Guru
                                  vExpertUser ModeratorsCommunity Warriors

                                  Not sure I get the question, when you import to the folder Datacenters, that should create the folders in the root of the vCenter.

                                  Perhaps you can add some screenshots to indicate what you mean ?

                                  • 14. Re: Export / Import selective Folder structure
                                    gtaylor1 Novice

                                    As you saw from the other thread you replied to me on, Mark's script was a little more comprehensive for the task I'm doing

                                     

                                    However, for the sake of anyone else finding this thread... This is how I want it to look after the folders are migrated:

                                    Capture.PNG

                                     

                                    However this is how it looked after I ran your modified script with  $startFolder = Get-Folder -Name Datacenters

                                    Capture.PNG

                                    Note: The script imported all sub-folders correctly, I created the above screenshot manually as I deleted the folders after the script failed to create the above folder structure.

                                    1 2 Previous Next