VMware Cloud Community
sureshadmin2011
Enthusiast
Enthusiast

Export / Import selective Folder structure

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!

Reply
0 Kudos
18 Replies
LucD
Leadership
Leadership

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
alanrenouf
VMware Employee
VMware Employee

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

Smiley Wink

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos
sureshadmin2011
Enthusiast
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 Smiley Happy

Reply
0 Kudos
sureshadmin2011
Enthusiast
Enthusiast

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

Reply
0 Kudos
LucD
Leadership
Leadership

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sureshadmin2011
Enthusiast
Enthusiast

Luc, i accessed the sybex download page http://spa.sybex.com/WileyCDA/SybexTitle/VMware-vSphere-PowerCLI-Reference-Automating-vSphere-Admini... 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.

Reply
0 Kudos
LucD
Leadership
Leadership

Did that original line produce error messages ?

And how did the path look when it gave errors ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sureshadmin2011
Enthusiast
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!

Reply
0 Kudos
CarlosDionizio
Contributor
Contributor

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

Reply
0 Kudos
gtaylor1
Contributor
Contributor

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.

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
gtaylor1
Contributor
Contributor

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?

Reply
0 Kudos
LucD
Leadership
Leadership

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 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
gtaylor1
Contributor
Contributor

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 Smiley Happy

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.

Reply
0 Kudos
LucD
Leadership
Leadership

Then you should use

$startFolder = Get-Datacenter -Name 'Melbourne'


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
gtaylor1
Contributor
Contributor

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.

Reply
0 Kudos
LividBliss
Enthusiast
Enthusiast

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

This worked for me:

$startFolder = Get-Folder -Name "vm"

Reply
0 Kudos
virtualinstall
Enthusiast
Enthusiast

This worked perfectly for me, many thanks Smiley Happy

Reply
0 Kudos