Skip to main content

Querying for nested folders with PowerCLI

Have you fought trying to query nested, duplicate-named folders?  Hopefully this will help solve the problem!  Suppose you have a VM folder-tree similar to this: 

So, how do you get the "\dotcom\linux\dev" folder using PowerCLI?  If you query for just "dev" then you can get an array of folders.

You can parse through the array and, using the parent object, traverse the tree backwards validating the folder names.  But, what if you have 100s of folders?  In my opinion, this is not an optimal approach.  We really need to do this:

This is great case for recursion.  In my words, recursion is a "stack" of operations.  When an operation completes its result is used by the next operation in the "stack".  Most importantly there has to be base-case which causes the last operation in the stack to return a valid result.  Then each operation can be popped off the "stack" and its result can be used by the previous operation until the final result is obtained.  What does all this mean?  Let's see what the "stack" for our query looks like:

stack[0] : return get-folder -name "dev" -location (folder result of stack[1])
stack[1] : return get-folder -name "linux" -location (folder result of stack[2])
stack[2] : return get-folder -name "dotcom" -location (folder result of stack[3])
stack[3] : return get-folder -norecursion   (this gets us the root folder for the Datacenter)

Since it's LIFO then stack[3] will be evaluated and the result will be used by stack[2]. Then stack[2] will be evaluated and the result will be used by stack[1].  Then stack[1] will be evaluated and the result will be used by stack[0].  Finally stack[0] will be evaluated and that will be nested folder we are trying to find.  Simple, right?  Here's the scripting (notice there are no loops):

# get folder location based on sub-folders
function getVcFolderByPath {

  if ($folderString -eq "") {
    return (get-folder -noRecursion)
  } else {
    $folderArr = $folderString -split "\\"
    $folderName = $folderArr[-1]
    $updatedFolderString = [string]::join("\", $folderArr[0 .. ($folderArr.length-2)])
    return (get-folder -name $folderName -location (getVcFolderByPath $updatedFolderString))

What is this doing you ask?  First is our base-case:

if ($folderString -eq "") {
  return (get-folder -noRecursion)

When we can't go back any further in the folderString then we must be at the root so return the root folder to be used by the previous operation in the "stack".  If we aren't at the last item then we need to split $folderString on the "\"

$folderArr = $folderString -split "\\"

Then, we can get the last folder name in the current folderString 

$folderName = $folderArr[-1]

Then, we create an updatedFolderString with the last folder name removed

$updatedFolderString = [string]::join("\", $folderArr[0 .. ($folderArr.length-2)])

Finally, we call ourselves again, this time sending updateFolderString

return (get-folder -name $folderName -location (getVcFolderByPath $updatedFolderString))

This will continue until updateFolderString is empty and we are at the base-case.  Looking back at the "stack" representation from above, here's what we have:

stack[0] : return get-folder -name "dev" -location (getVcFolderByPath "\dotcom\linux")
stack[1] : return get-folder -name "linux" -location (getVcFolderByPath "\dotcom")
stack[2] : reutrn get-folder -name "dotcom" -location (getVcFolderByPath "")
stack[3] : return get-folder -norecursion

Like I said, simple!  Here's what it looks like when we call the function.

This does assume there is only one Datacenter.  As with all scripts, your mileage may vary depending on your environment.  Comments/Suggestions are welcome.  Happy Querying!

Since seems like using a sledgehammer to hang a picture so I'd thought I'd include the easy for-loop method as well:

# get folder location based on sub-folders
function getVcFolderByPath {

  $folder = get-folder -norecursion

  $folderArr = $folderString -split "\\"  
  for ($i=1 ; $i -lt $folderArr.length ; $i++) {
    $folder = get-folder -name $folderArr[$i] -location $folder
  return $folder


And then call it just like before.

The information is the same.


Popular posts from this blog

Reconnecting ESXi servers with PowerCLI

We recently needed to re-ip our vCenter servers; each with ~200 ESXi servers which needed to be reconnected - thank goodness for PowerCLI.  Since there isn't a "Reconnect-VMHost" cmdlet provided by PowerCLI we needed to check the HostSystem Object at and to see what methods were available (hint: there's a ReconnectHost_Task method which will do the trick).  We can still leverage the "Get-VMHost" cmdlet to return the disconnected ESXi servers and then call the ReconnectHost_Task method to reconnect each ESXi server.  The code is fairly short:

Get-VMHost -state Disconnected | foreach-object {
  $vmhost = $_
  $connectSpec = New-Object VMware.Vim.HostConnectSpec
  $connectSpec.force = $true
  $connectSpec.hostName = $
  $connectSpec.userName = 'root'
  $connectSpec.password = 'MySecretPassword'

Parsing XML with VMware Orchestrator

For my first blog I thought I would start with something easy - parsing XML using VMware Orchestrator (a.k.a vCO)!  I started playing with vCO in September 2012 for a "Cloud" project so I still consider myself a newbie - if you happen across this post and find something incorrect or something that could be done better then please don't hesitate to speak up.

Since I can't post our actual XML, I'll be using the following XML which will give the gist of how to parse for elements & attributes.

<?xml version="1.0" encoding="UTF-8" ?>
  <person firstname="Jack" lastname="Smith" age="40">
    <phone type="home" number="1234567890" />
    <phone type="cell" number="1234567891" />
<sport name="basketball" position="shooting guard" />
  <person firstname="Jill" lastname="Smith" age=&q…