Automation

 View Only
Expand all | Collapse all

Export / Import selective Folder structure

  • 1.  Export / Import selective Folder structure

    Posted Sep 15, 2011 03:40 PM

    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!



  • 2.  RE: Export / Import selective Folder structure

    Posted Sep 16, 2011 11:16 AM

    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.



  • 3.  RE: Export / Import selective Folder structure

    Posted Sep 19, 2011 05:16 AM

    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 :smileyhappy:



  • 4.  RE: Export / Import selective Folder structure

    Posted Sep 19, 2011 06:39 AM

    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.



  • 5.  RE: Export / Import selective Folder structure

    Posted Sep 19, 2011 10:39 AM

    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.



  • 6.  RE: Export / Import selective Folder structure

    Posted Sep 19, 2011 10:47 AM

    Did that original line produce error messages ?

    And how did the path look when it gave errors ?



  • 7.  RE: Export / Import selective Folder structure

    Posted Sep 19, 2011 11:28 AM

    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!



  • 8.  RE: Export / Import selective Folder structure

    Posted Feb 25, 2014 12:48 PM

    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??



  • 9.  RE: Export / Import selective Folder structure

    Posted Jun 04, 2015 06:36 AM

    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.



  • 10.  RE: Export / Import selective Folder structure

    Posted Jun 04, 2015 07:35 AM

    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



  • 11.  RE: Export / Import selective Folder structure

    Posted Jun 04, 2015 10:57 PM

    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?



  • 12.  RE: Export / Import selective Folder structure

    Posted Jun 05, 2015 05:22 AM

    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 ?



  • 13.  RE: Export / Import selective Folder structure

    Posted Jun 05, 2015 05:31 AM

    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 :smileyhappy:

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

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

    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.



  • 14.  RE: Export / Import selective Folder structure

    Posted Jun 05, 2015 05:42 AM

    Then you should use

    $startFolder = Get-Datacenter -Name 'Melbourne'



  • 15.  RE: Export / Import selective Folder structure

    Posted Jun 05, 2015 05:48 AM

    Now I swear I tried that earlier this morning and it failed with an error... I since discovered Mark's script and found it worked a bit better for doing the whole migration so I've been working with that for the rest of the day...

    Appreciate the assist, and hopefully anyone else finding this thread is helped out by it.



  • 16.  RE: Export / Import selective Folder structure

    Posted Aug 20, 2015 07:16 PM

    The BlueFolder or root VM folder  is "vm" by default - Get-Folder

    This worked for me:

    $startFolder = Get-Folder -Name "vm"



  • 17.  RE: Export / Import selective Folder structure

    Posted Jan 20, 2016 02:12 PM

    This worked perfectly for me, many thanks :smileyhappy:



  • 18.  RE: Export / Import selective Folder structure

    Broadcom Employee
    Posted Sep 16, 2011 11:24 AM

    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

    :smileywink:



  • 19.  RE: Export / Import selective Folder structure

    Posted Sep 19, 2011 05:18 AM

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