Folder by Path

There seem to be many vSphere environments where the same foldername is used multiple times. A blue folder with the name Servers is quite common for example.

If you need to retrieve such a folder with the Get-Folder cmdlet, you will have to walk the path to the folder leaf by leaf and use the Location parameter. It would be handier if you could just specify the path to the folder and retrieve the folder like that.

The following is a small function that will allow you to do just that.

Update February 18th 2016: In some situations the function might return folders with the same name from different location. Fixed by adding NoRecursion on line 48

The Script

Annotations

Line 31-36: Takes care of Single or Multiple mode. In multiple mode, the function will look for the folder on each connected vSphere server. See Get-PowerCLIConfiguration.

Line 38: Loop over all the vSphere servers.

Line 39: The function can accept more than one path. This loops through all the provided paths.

Line 40: The function starts looking at the root folder. This is the hidden Datacenters folder.

Line 41: The provided path is split into separate leaves.

Line 42: The Path can have a datacenter as a leaf, hence the functions uses the Get-Inventory cmdlet to find the children.

Line 43-45: Skip the hidden “vm” folder

Line 47-49: When all the children are retrieved, the Where-clause will only pass the Folder objects back.

Line 48: Notice that we have to back one leaf, hence the $root.Parent on the Location parameter.

Sample Usage

I created the function to avoid this problem.

The function is quite simple to use.

This gives me exactly the folder I want.

Now it becomes easy to fetch for example all the VMs that are located in a specific folder.

Bonus: a New-VIProperty

Retrieving folders by path became easy, it would now be interesting to see what the path of a VM is. That’s where you can use the following New-VIProperty.

It creates a new property called BlueFolderPath and that property will show you the blue folder path where a VM is located.

32 Comments

    Everton Poyato

    Luc,

    I have a vCenter ‘VC1’ with with two DataCenter: DC1 and DC2
    Both have a folder called ‘PDI’

    When I connect to this vCenter and use Get-FolderByPath it returns the way I want:

    $ DC = ‘DC1’
    $ Vmfolder = ‘PDI’

    $ Folder = $ DC + “>” + $ vmfolder
    $ Vmfolder = Get-FolderByPath -Path $ folder -Separator ‘>’

    But when I use the new-vm command it is not binding to the folder of that DataCenter that I have specified:

    New-VM -VMHost $ hostesx -Datastore $ datastoreLun -Location $ vmfolder -Name $ hostname -template $ template -OSCustomizationSpec $ hostname

    New-VM: 01/12/2016 12:25:54 New-VM The specified parameter ‘Location’ expects a single value, but its name criteria ‘PDI’
    Corresponds to multiple values.

    kevin

    I have a similar issue….I am moving resources from 1 vcenter to another. need to make sure the VMs and templates end up in the correct folder when completed. have duplicate folder names in the VM folder structure. move-inventory doesn’t support folderID as a vi-container, any ideas on how to accomplish this?

    Franics

    is there any script which can create folder from excel file?

      LucD

      Yes, you can use something like

      New-Item C:\MyFolder -Type Directory

      If you want to create a number of directories from a CSV file, you do something like this

      Import-Csv C:\MyFolders.csv -UseCulture | %{
      New-Item -Path $_.FolderName -ItemType Directory
      }

    Bala

    When I tried to retriev Subfolder Path details using the Get-FolderByPath Function its giving me error, Not sure why its giving below error:

    PowerCLI C:\Users\e3006122\Desktop> Get-FolderByPath -Path “cape/First”
    Get-Inventory : 15-12-2015 17:07:08 Get-Inventory Inventory with
    name ‘First’ was not found using the specified filter(s).
    At C:\Users\e3006122\Documents\Sabre Documents\PowerCLI
    SCripts\GetFolderbyPath.ps1:42 char:19
    + $root = Get-Inventory -Name $_ -Location $root -Server $vc
    -NoRecursio …
    +
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (:) [Get-Inventory], VimExceptio
    n
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimA
    utomation.ViCore.Cmdlets.Commands.GetInventory

    If I try to retrieve the Foldername using Get-Inventory its working fine.

    samir

    hi,

    I want to create new folders under folders

    like

    Clients
    -Client1
    -DC
    -Exchange
    -SQL
    -Client2
    -DC
    -Exchange
    -SQL
    -Client3
    -DC
    -Exchange
    -SQL
    -Client4
    -DC
    -Exchange
    -SQL

    is there anyway that we can do this using script?

    Thanks in advance!

      LucD

      Hi Samir,
      Yes, that can be done.
      In fact there are several options.

      Example 1

      $tgtFolders = 'Clients/Client1','Clients/Client2','Clients/Client3','Clients/Client4'
      $subfolders = 'DC','Exchange','SQL'

      foreach($fld in $tgtFolders){
      $fldObj = Get-FolderByPath -Path $fld
      foreach($newlfd in $subfolders){
      New-Folder -Location $fldObj -Name $newfld -Confirm:$false
      }
      }

      Example 2

      $baseFolder = 'Clients/Client'
      $numberOfClients = 4
      $subfolders = 'DC','Exchange','SQL'

      1..$numberOfClients | %{
      $fldObj = Get-FolderByPath -Path "$($baseFolder)$_"
      foreach($newlfd in $subfolders){
      New-Folder -Location $fldObj -Name $newfld -Confirm:$false
      }
      }

      I hope that helps,
      Luc

    Aaron

    Is there any way to do this with Datastore folders?

    Johan Donders

    Great .. it works … thx a lot.
    BTW, my root folder is called “centre de données”

    Ehren

    Hi,

    I think we have found an issue where this is not working and wondered if you had any input.

    If we have two folders named “SuperFolder” and run get-folderbypath and specify the path to one of them it returns them both such as below:

    Get-FolderByPath -Path “it services admin/it services” | Get-FolderPath

    Name Path
    —- —-
    SuperFolder Datacenter\Org SuperFolder Datacenter\Org\SelfManaged\IT Services

    Do you have any thoughts on why?

    Thanks a lot.

      Ehren

      The formatting went crazy and I mistyped something, for clarity:

      Get-FolderByPath -Path “Datacenter/Org” | Get-FolderPath

      Name :: Path
      SuperFolder :: Datacenter\Org
      SuperFolder :: Datacenter\SelfManaged\Org

      So it seems like the “selfmanaged” folder being in the ‘middle’ of the specified path (Datacenter and Org) seems to trip it up as the root and end are the same with other folders inbetween.

      Would appreciate your thoughts.

      Thanks

    Peter

    Hi,

    Looks like I may have found the issue regarding returning multiple folder objects within the same root folder.

    Scenario:
    Datacenter/Folder/Folder1
    Datacenter/Folder/Folder2/Folder1
    Issue:
    run get-folderbypath -path Datacenter/Folder/Folder1
    function returns BOTH ‘Folder1’ objects

    run get-folderbypath -path Datacenter/Folder/Folder2/Folder1
    function returns correct ‘Folder1’ object only

    FIX:
    Modify line 48 by adding ‘-NoRecursion’ to the end of the line
    Get-Folder -Name $_.Name -Location $root.Parent -Server $vc -NoRecursion

    run get-folderbypath -path Datacenter/Folder/Folder1
    function returns correct ‘Folder1’ object only

    Do you see any issues that modifying this line could cause?

      LucD

      Great find !
      No, I don’t see any obvious issues this might cause.

    Peter

    Hi Luc,

    I have the following scenario :
    Datacenter/folder/folder1
    Datacenter/folder/folder2/folder1

    when I use this function
    get-folderbypath -path Datacenter/folder/folder1
    it returns both ‘folder1’ folders

    Any ideas why it is picking up both?

    AndyP

    Hi LucD – is there a way to import a folder structure using the the path to import folders with the same name under different levels in the folder structure?
    eg import from a csv that contains name and path in seperate columns
    Name Path
    Prod folder_UniqueName1\ServiceLevel\Prod
    Prod folder_UniqueName1\ServiceLevel\Prod

    cleaning advice

    Hello! I know this is somewhat off topic but I was wondering if you knew where I could locate
    a ccaptcha plugin forr my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one?
    Thanks a lot!

    Feel free to urf to myy site; cleaning advice

    Alberto

    Hey Luc thanks for all this..
    I was able to export the VMs and their folder structure with your Get-ParentName function but here is the problem I am having right now.
    I am migrating all those VMs to a new VC where the same folder structure exists but there are duplicated folder names of coarse.
    All my VMs by default will go into the Discovered Virtual Machine folder after the migration. Do you have any other function to import the CSV file I have and move the VMs in the Discovered VM folder to their individual folder using the full blue folder path this way I can avoid the duplicated names issue??

    Maybe I can use the Get-Folderbypath function.

    Thanks in advance,
    Alberto

      LucD

      Did you already check out Export/Import VM Folder Structure to new vCenter ?
      It discusses nearly the same thing you are trying to do.
      It contains several pointers to export and import scripts.
      Let me know if those help.

    David Roger

    Hi.
    I think you can replace “$root = Get-Folder -Name Datacenters -Server $vc” by “$root = Get-Folder -NoRecursion -Server $vc”.
    Name of root folder is not always “Datacenters”.
    Thanks for this article.

      LucD

      Thanks for the tip !
      Have you ever seen another name for the root folder ?
      I always saw “datacenters” in the environments where I tested.

    Mike

    @admin
    I am using vSphere Client Version 5.0.0 build 455964 and Power CLI version 5.0 build 435427. I would give you a copy of my output but unfortunately the server details are confidential. Thanks for your help.

    Mike

    @admin
    Thank you for that code. Unfortunately it still doesn’t seem to have returned any folder details for those VMs that are in vApp groups. They still just come through as blank. Thanks for the suggestion anyway, much appreciated.

      admin

      @Mike, strange, I get the path for VMs in a vApp.
      Which vSphere and PowerCLI versions are you using ?

    Mike

    Hi LucD,

    Thank you very much for this script, it is much appreciated. I have a problem with a couple of VMs that are in vApp groups and they are reporting as null for the BlueFolderPath. Is there a way around this?

    Many Thanks.

      admin

      @Mike, does this version produce the path you want to see ?

      New-VIProperty -Name 'BlueFolderPath' -ObjectType 'VirtualMachine' -Value {
      param($vm)

      function Get-ParentName{
      param($object)

      if($object.Folder){
      $blue = Get-ParentName $object.Folder
      $name = $object.Folder.Name
      }
      elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){
      $blue = Get-ParentName $object.Parent
      $name = $object.Parent.Name
      }
      elseif($object.vApp){
      $parentFolder = Get-VIObjectByVIView (Get-View $object.VApp.ExtensionData.ParentFolder)
      $blue = Get-ParentName $parentFolder
      $name = $parentFolder.Name
      }
      elseif($object.ParentFolder){
      $blue = Get-ParentName $object.ParentFolder
      $name = $object.ParentFolder.Name
      }
      if("vm","Datacenters" -notcontains $name){
      $blue + "/" + $name
      }
      else{
      $blue
      }
      }

      (Get-ParentName $vm).Remove(0,1)
      } -Force | Out-Null

    Grzegorz Kulikowski

    Hi Gregg, Luc, This is the first time i see this script from Luc as he mentioned about it on communities today/yesterday. Secondly, believe me that if somebody writes code about the same stuff it does not mean that he has even seen somebody else code before. It’s just how scripting/coding looks. You CAN have the same ideas and try to implement them in a similar way, i guess that’s nothing wrong with that. That said, this script is for completely different thing, i don’t even see how did you manage to tell that “i have copied his script” ?:) So next time you will accuse somebody of taking somebody else credits think twice as i do not feel “cool” about being accused of that. All my code on my site is written by me and if i want to mention about somebody approach i always put link,quotes etc… . That being said 😉 Cool script Luc, as always 😉

      LucD

      @Grzegorz, as I already commented, I don’t consider your function a copy. Your script is set up completely different.

      Let’s stop any further comments on this, both parties have had their say in this.

      Happy scripting Grzegorz 🙂

    Flux

    This script doesn’t seem to be working for me. I’ve tried using PowerCLI 5.1 Release 1 and PowerCLI 5.0.1 against vCenter Server 5.0U1 and I get the following:

    Get-Folder Folder with name ‘Datacenters’ was not found, using the specified filter(s).

    Any tips on where to look?

    Thanks,
    Flux.

      LucD

      @Flux, just to make sure, you are running this while connected to a vCenter ?

    Gregg

    Hi

    I think someone has replicated your work and claimed it’s theirs

    psvmware.wordpress.com/2012/09/14/get-vmfolderpath-to-check-in-which-vm-folder-our-vm-resides/

      LucD

      Hi Gregg, the function starts from the same idea, but it is written differently. So I don’t think it’s a replication of my script.
      Thanks for letting me know.

    Steven Garcia

    Very Cool. Thanks, LucD!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Buy the Book