I am currently in the process of creating a script to automate batch creation of our VMs in various vCenters, and have reached the point where the VM Creation is doing exactly what I need, but it is still creating the VM in the root folder within the vCenter.
I started looking around for solutions on how to move the VM to the correct folder, or create the VM in the correct folder from the start, and came across the Get-FolderbyPath function created by LucD.
I started playing around with the function, and am struggling to get it to give me exactly what I need.
The biggest part of the issue is that our folder structure within each vCenter is a.) very similar to each other, and b.) each vCenter contains many of the same folder names
Our Folder layout is Datacenter/Company/Project/Environment, and so as a rough example we have folders called:
So if I am connected to only the Paris vCenter and run Get-FolderbyPath -Path "Paris/Company/Project/Lab" I get only one result and if I pipe that to a Get-VM I can verify that I only see the VMs I expect to be seeing.
But if I am connected to vCenter using -allLinked and run the same command I get multiple results, and I believe I have narrowed down what is happening, but I am not sure why.
When running the Get-folderbypath when connected to multiple vCenters the below is what is happening
If I am looking for "Paris/Company/Project/Lab" as a path, when the script runs through the Paris vCenter I only get one result (the expected result), but then the script runs through against the Ottawa vCenter, and if it does not have an exact "Company/Project/Lab" path it returns any folder that is named "Lab" no matter where its located, so the Folder results are as below:
The very strange part is that we also have folders such as below that are NOT being returned by the Get-Folderbypath function:
But if we do have an exact "Company/Project/Lab" Path in both vCenters the results are
and in this case the
Paths are not shown.
So I am trying to figure out how to get the Get-folderbypath function to only give me Paris/Company/Project/Lab when connected to multiple vCenters or if that exact path doesn't exist, return 0 entries so I know it doesn't exist so I can instruct my script to create the new folder as required.