VMware Cloud Community
BostonTechGuy
Enthusiast
Enthusiast
Jump to solution

Creating Folder Structure

Trying to find a quick way to import all the VM & Templates (aka BLUE Folders) from Site A to Site B.  Each Site has its own vCenter with multiple "Datacenters" located inside.  I just want all the folders and subfolders of one Datacenter.. Now that explained here's whats happening

Exporting Site A folders using this line of code is working perfectly:

Get-Datacenter "Datacenter1" | Get-Folder | Get-FolderPath | Export-Csv "C:\Scripts\learning\folders.csv" -NoTypeInformation

I get a nice CSV file with NAME and PATH headings with all the names and paths of each folder.

Now when I try to Create the folders at Site B, it doesnt work most of the time. All these failed:

Import-Csv $importlist -UseCulture | %{

    New-Folder -Name $_."Name" -Location $_."Path"

}

Import-Csv $importlist -UseCulture | %{

    New-Folder -Name $_."Name" -Location (Get-Folder -Name $_."Path")

}

Import-Csv $importlist -UseCulture | %{

    New-Folder -Name $_."Name" -Location (Get-Datacenter "Datacenter1" | Get-Folder -Name $_."Path")

}

Now.. if I do that last script AND remove "DATACENTER1" from the cell in CSV file under Path, the root folders get created.  Still no joy on the subfolders.

Now I have reviewed other code in other posts on this site.  And several from "some of people who are on this site offen and wrote some books" (you know who you are Smiley Wink ).  None of them work for me.  All I need is to have several hundred (yes hundred) folders created with multi-sub levels deep.  Some case 5 or 6 levels deep.. IE:  DATACENTER\VM\ROOT FOLDER\ SUB LEVEL1\SUB LEVEL2\SUB LEVEL3\SUB LEVEL4\ <here be vms>

Thanks,

Boston Tech Guy

Tags (1)
Reply
0 Kudos
21 Replies
mcity
Contributor
Contributor
Jump to solution

I am using the same script, but subfolders are created randomly, with errors as attched

 

 

 

Reply
0 Kudos
mcity
Contributor
Contributor
Jump to solution

This script did it for me, thanks FDO

$folder_list = get-content .\vm_folder_list.txt
$dcname = "Maki-DC"
$datacenter = Get-Datacenter -Name $dcname
# Define VM root folder
$rootFolder = $datacenter | Get-Folder -Name vm -Type VM
Foreach ($folderpath in $folder_list){
$fullpath = $folderpath
# Split fullpath into individual folder names
$directorylist = $fullpath.split('\')
# Skip datacenter folder name
$directorylist = $directorylist | Where {$_ -ne $dcname}
$parentfolder = $rootFolder
# Loop throught the list of directories and create them if not found.
# Folder are created row by row, from left to right
# Datacenter Root \ Subfolder1 \ Subfolder2 \ Subfolder3
# Once the folder is created it becomes the parent for subfolder.
Foreach ($directory in $directorylist){
$current = Get-Folder -name $directory -ErrorAction SilentlyContinue
if ($current -eq $null){
Write-Host "Creating new folder $directory" -ForegroundColor Green
$parentfolder = New-Folder -Name $directory -Location $parentfolder
}else{
Write-host "Skip - Found folder $directory" -ForegroundColor Yellow
$parentfolder = $current
}
}
}
Reply
0 Kudos