About vCenter Server Folders and how to Copy them

Recently, while working on a new deployment, it was determined to copy the folder structure from an existing vCenter Server to a new instance.
As the folder structure is complex and the vCenter Servers were located in separated environments, exporting and importing the folder structure seems the way to go.
Before continuing, let’s see how folders are organized in vCenter Server.

Each folder in vCenter Server has properties, which can be observed using one of the following commands:

PS> Get-Folder | Select *
PS> Get-View -ViewType Folder

In vCenter Server you can create Folders directly under the vCenter Object and under each Datacenter. Folders created under the vCenter Object will appear on each of the four tabs (“Host and Clusters”, “VMs and Templates”, “Storage” and “Networking”).
On the Datacenter level, you can create different folder structures for each of the four tabs. After creating a new Datacenter object, vCenter Server creates four hidden folders, named “host”, “network”, “datastore” and “vm”, which function as parent folders for each tab.

In the diagram (Fig. 1) I’ve tried to present an overview for a vCenter Server with two Datacenters (Datacenter1 and Datacenter2) and a few folders like “Discovered virtual machine” and ”Templates”.
On the far left is the root object which is always called “Datacenters”.
The folders marked with a red X are not visible in the vCenter GUI. Visible folders are marked with a blue folder icon. The properties “Name”, “Id” and “ParentId” are shown in the diagram. Each folder is uniquely identified with it’s “Id” and “ParentId”.

Fig. 1

The following line shows the hidden folders on the Datacenter level:

PS> Get-View -ViewType Folder | 
Where-Object {$_.Parent -like "Datacenter*"} | 
select Name,Parent,Childtype
Name      Parent                   ChildType
----      ------                   --------- 
host      Datacenter-datacenter-21 {Folder, ComputeResource} 
network   Datacenter-datacenter-21 {Folder, Network, DistributedVirtualSwitch}
datastore Datacenter-datacenter-21 {Folder, Datastore, StoragePod} 
vm        Datacenter-datacenter-21 {Folder, VirtualMachine, VirtualApp}

The output also shows the child types the folders can contain. E.g. the “vm” folder can contain virtual machines, vApps and also sub-folders.
As we already know, the property “Parent” connects a folder to it’s parent folder.

The next line of code will show all folders in “VMs and Templates” section:
Note: Instead of the property “Id” as used by Get-Folder, Get-View uses “MoRef” for the unique identifier.

PS> Get-View -ViewType Folder | 
Where-Object { $_.childtype -like "VirtualMachine"} | 
select Name,Moref,Parent
Name                       MoRef             Parent 
----                       -----             ------ 
DKM                        Folder-group-v163 Folder-group-v22 
Level1                     Folder-group-v164 Folder-group-v163 
Level2                     Folder-group-v165 Folder-group-v164 
VRM                        Folder-group-v179 Folder-group-v22 
Level1                     Folder-group-v180 Folder-group-v179 
vm                         Folder-group-v22  Datacenter-datacenter-21
Discovered virtual machine Folder-group-v34  Folder-group-v22 
Templates                  Folder-group-v49  Folder-group-v22 
Endpoint                   Folder-group-v65  Folder-group-v22 
Blueprint                  Folder-group-v66  Folder-group-v22

Fig. 2

Now, with this knowledge we can create an export file, which can be used to import  the folder structure in the new vCenter Server.

The export file for the given example (Fig. 2) looks like this. Each line starts with the sub-folder on the deepest level.

/DKM</span>
/Level1/DKM</span>
/Level2/Level1/DKM</span>
/VRM</span>
/Level1/VRM</span>
/Discovered virtual machine</span>
/Templates</span>
/Endpoint</span>
/Blueprint</span>

A second script reads the export and builds the folder structure on the new vCenter Server.

Both scripts can be found here. Scripts are still very basic and the current version of the scripts has the following limitations; only one Datacenter is allowed and only folders in the “VMs and Templates” are in scope.

Before running a script, connect to only one vCenter Server and don’t forget to disconnect after running the export script!

While working on the scripts, it came up to me that Configuration drift can also hit vCenter Folder structures, so the export script was also the base for writing a first draft for a new Vester Test. Although still working on the “fix” section, it is helpful to know that the folder structure has changed.

One Response to About vCenter Server Folders and how to Copy them

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: