-
1. Re: Export / Import selective Folder structure
LucD Sep 16, 2011 4:15 AM (in response to sureshadmin201110141)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 -UseCultureThe 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 Sep 16, 2011 4:24 AM (in response to sureshadmin201110141)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 Sep 18, 2011 10:16 PM (in response to LucD)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 Sep 18, 2011 10:18 PM (in response to alanrenouf)Thanks Alan. That book is a marvellous work! Adopted many of them for my daily work.
-
5. Re: Export / Import selective Folder structure
LucD Sep 18, 2011 11:38 PM (in response to sureshadmin201110141)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 Sep 19, 2011 3:39 AM (in response to LucD)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 Sep 19, 2011 3:46 AM (in response to sureshadmin201110141)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 Sep 19, 2011 4:29 AM (in response to sureshadmin201110141)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 likeName Path Test01 dc01\folder1
Sorry for that! Your function works perfect! -
9. Re: Export / Import selective Folder structure
CarlosDionizio Feb 25, 2014 4:48 AM (in response to LucD)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 Jun 3, 2015 11:36 PM (in response to LucD)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 Jun 4, 2015 12:34 AM (in response to gtaylor1)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 Jun 4, 2015 3:57 PM (in response to LucD)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 Jun 4, 2015 10:22 PM (in response to gtaylor1)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 Jun 4, 2015 10:31 PM (in response to LucD)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:
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.