Afternoon,
We are building a new vCenter 5.1 Environment. Want to duplicate the VM Folder structure from the 4.1 vCenter (Folders under INVENTORY>VMs and TEMPLATES). All we need are the folders and Sub Folder Structure.
VCENTER >
DATACENTER>
PARENT FOLDER 1>
SUB FOLDER 1>
SUB FOLDER 2>
PARENT FOLDER 2>
SUB FOLDER 1>
SUB FOLDER 2>
Etc etc...
How would it be the best aproach to do this with PowerCLI?
I have seen a few other posts on this topic, most involved a VM Audit, which is not what we need. Just the complete Folder Structure of a 4.1 vCenter (including all sub folders). Nothing Else.
Goals:
Thanks,
BostonTechGuy.
Did you already have a look at Re: Export / Import selective Folder structure and Re: Folder migration
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I was hoping you would reply. Thank you!
I did try those scripts as well as the one located on your site here:
http://www.lucd.info/2010/10/21/get-the-folderpath/
The output became questionable on my end. The one from you site got very very close but the formating was completely different from what everyone else had as a result. What I got was:
Name: <name>
Path: <Path>
Type: <type>
Name: <name>
Path: <Path>
Type: <type>
Name: <name>
Path: <Path>
Type: <type>
Etc etc etc
Instead of a nice table, I got single lines. It was odd. This is the code I ran, I kept it 99.9999% (I added one line ) from your code.:
param(
[parameter(valuefrompipeline = $true,
position = 0,
HelpMessage = "Enter a folder")]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl[]]$Folder,
[switch]$ShowHidden = $false
)
begin{
$excludedNames = "Datacenters","vm","host"
}
process{
$Folder | %{
$fld = $_.Extensiondata
$fldType = "yellow"
if($fld.ChildType -contains "VirtualMachine"){
$fldType = "blue"
}
$path = $fld.Name
while($fld.Parent){
$fld = Get-View $fld.Parent
if((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden){
$path = $fld.Name + "\" + $path
}
}
$row = "" | Select Name,Path,Type
$row.Name = $_.Name
$row.Path = $path
$row.Type = $fldType
$row
}
}
}
$vcserver = "name of my vcenter"
Connect-VIServer $vcserver
Get-Folder | Get-FolderPath
Thanks,
BostonTechGuy
Try changing that last line to
Get-Folder | Get-FolderPath | Format-Table -AutoSize
That should force the results to be on 1 line per folder
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you. That worked. Now I am having issue exporting it to a CSV.
When I add Export-CSV cmd to a CSV file I get coded output. When I OUT-FILE cmd to a TXT file I get the correct data. However the table format is not correct for Excel to recognize it cleanly when I import the TXT file. While a headache for formating, I would like to correct this, make it clean and automated. I have a couple of these for several differnet vCenters.
Works for TXT file:
Get-Folder | Get-FolderPath | Format-Table -AutoSize | Out-File "C:\Scripts\folder_structure\results.txt"
Doesnt Work for CSV:
Get-Folder | Get-FolderPath | Format-Table -AutoSize | Export-CSV "C:\Scripts\folder_structure\results.csv"
Thanks,
BostonTechGuy
If you want to export, leave out the Format-Table
Get-Folder | Get-FolderPath | Export-CSV "C:\Scripts\folder_structure\results.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, you are still the PowerCLI Man!
So far so good. Now I used a simple For Each loop to build out the folders in the root location:
Import-Csv $importlist -UseCulture | %{
New-Folder -Name $_."Name" -Location (Get-Folder -Name 'vm')
}
Works perfectly fine for just root folders in VMs and Templates. How would I modify this to create the Sub Folders to get the structure I am looking for? Also at the same time leverage the PATH Column in the output that I got from the script used above.
Thanks
BostonTechGuy
You might want to have a look at my Get the folderpath post. It will provide you with the full path of the folders.
Gabrie used that function in his Cheap disaster recovery post. Have a look at his Export- and Import-Folders scripts.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank for all the help.
Your post on the GET-FolderPath is what I have been using from the start and works perfectly. I downloaded Gabe's scripts. I am in the process of reviewing the scripts now. Priority of the project changed and need get the creation folders done last night. I ended up using a nested cmdlet for each folder that had child and grandchild sub folders. Simple, blunt and not pretty but worked.
New-Folder -Name "GrandChild Folder Name" -Location (Get-Folder -Name "Child Folder Name" -Location (Get-Folder -Name "Parent Folder under VMs and Templates" -location (Get-Folder -Name 'vm')))
That is the base logic for the cmdlet. I used the command below to use imports of the names of folders from a CSV file:
$importlist = "C:\Scripts\name of import file with names.csv"
Import-Csv $importlist -UseCulture | %{
New-Folder -Name $_."Name" -Location ... etc etc
}
Again this worked. I was able to create about 100 folders of various levels inside VMs and Templates in about 30 mins. This would have taken hours otherwise. Also I need to do this again to three more vCenter environments.
LuD, again thank you for all the help.
Thanks,
BostonTechGuy
P.S. On a side note, about vCenter 5.1 Web Client. The interface for creating folders is slow and clunky. From here on out I am only using PowerCLI New-Folder command if I ever have to make more than one folder at a time.
BT
I came across this, as I am trying to accomplish the same thing you did, I am not a PS guru, so if you can reply to this post and let me know what steps you took in other to accomplished this, I would appreciated, I would like to follow your step by step , so I dont have to manually write all folder with subfolders, like your case, they are a bunch of them
Thanks for your time and help.
I am just seeing this now. Let me get back to you in the morning. See how I can help.
FBORGES555,
Following up on this post per your request. Give me a day or so to review the structure of the script so I can properly respond to your question. Its gotten to the point where this script has been set and forget to me. Especially as I move from client to client doing migrations.
In the meantime some great places to start. I consider myself an "advanced novice" when it comes to Powershell. IMHO Powershell is well.. that.. powerful. I am currently controlling an infrastructure of a 1 Billion Dollar pharmaceutical company with a staff of 2. You dont do that without alot of automation, which Powershell provides. VMware was smart to add PowerCLI as an add on module to Powershell. Instead of creating a new book, then tagged along with Microsoft on Powershell. Its fantastic. I use PowerCLI almost every day, so I use Powershell everyday as well when you really look at it.
A program I cannot recommend enough is PowerGUI. Its an application that operations a lot like Visual Studio. It was originally written by Quest Software, who was bought out by Dell. No idea what Dell has in store for the application. I hope they continue grow the app, but for now you can download it here: http://en.community.dell.com/techcenter/powergui/m/bits/20439049 Also please download the PowerPacks for PowerCLI. Latest release is here: http://en.community.dell.com/techcenter/powergui/m/powerpacks/20438869 Seems there is a beta for later ESXi builds. As I am reading the site.. I really really hope Dell does something with PowerGui. Its a great application.
Please fee free to contact through the forum on what your time table is for the folder migration and creation. Will get you a step by step guide on how to get this to work.
Thanks,
Boston Tech Guy
Hi Boston " you are the man"
I cant thanks you enough for mentoring and your advices, as I am trying to take over the infrastructure, the need for a tool (PS) that will provide me with an upper hand is priceless, I am creating the DR site and I would like to mimic the primary we currently have, I have the basic already set Vcenter->datacenter, so I just need the folders that will populate the VM and temples without VM, just the folder.
I used the same script from LucD to create my CSV with the following format in it
"folder Name","my_datacenter_name\folder\folder","blue"
from here I would like to import each line directly below my datacenter
--| Vcenter
--|my_datacenter_Name
Insert folder structure from CSV file here...........
Again, I cant thank you enough for all this help, and a day or so is perfectttttt, again thanks a bunch for your time and patience.
Like I said priceless
FB