This post is one of those that would need to be done prior to the creation of clusters. As the title indicates we will be creating all of the folder objects in this post. The concept behind the folders is pretty simple, we just need to understand that there are 4 different folder types:
1. Network
2. HostAndCluster
3. Datastore
4. VM
Let's jump right in...
param( [string] $SourceVIserver = $(Read-Host -prompt "Source vCenter?"), [string] $DestinationVIserver = $(Read-Host -prompt "Destination vCenter?") ) $svc=connect-viserver $SourceVIserver $dvc=connect-viserver $DestinationVIserver $folderpaths = @() $excludedNames = "Datacenters","vm","host","Datastore","Network" $folders = get-folder -server $svc|?{$excludednames -notcontains $_.name} foreach ($folder in $folders) { $output = ""|select name, path, type $output.name = $folder.name $output.type = $folder.type $path = $folder.name $fld = $folder.extensiondata while ($fld.parent) { $fld = get-view $fld.parent -server $svc if ($excludedNames -notcontains $fld.name){$path = $fld.name + "\" + $path} } $output.path = $path $folderpaths += $output } $folderpaths = $folderpaths|sort path foreach ($folderpath in $folderpaths) { switch ($folderpath.type) { "Network" {$parent = get-folder -server $dvc -name "Network"} "HostAndCluster" {$parent = get-folder -server $dvc -name "Host"} "Datastore" {$parent = get-folder -server $dvc -name "Datastore"} "VM" {$parent = get-folder -server $dvc -name "vm"} } $fullpath = ($folderpath.path.split("\")) $depth = $fullpath.count for ($i = 1; $i -lt $depth; $i++) { if ($depth - $i -eq 1){new-folder -location $parent -name $folderpath.name} else{$parent = get-folder -location $parent -name $fullpath[$i]} } } disconnect-viserver * -confirm:$false -force
Breaking this down:
Lines 1-6, source and destination parameters
Lines 8-9, initialize variables
Line 10, get all folders from source vCenter, exclude system parent folders
Lines 11-25, loop through nested folders to create an array object with the full path for each folder
Lines 27-44, Create folders in their proper location
Line 46, disconnect from vCenters