How to find a VM folder path with PowerCli ?

Ok, here’s the riddle 🙂 . I can not use the search feature on vCenter because the enterprise firewall rule does not allow the specific – port 10443 BTW – and you need to find it among that folder jungle with several folder levels.

Here’s a quick script ready to make it for you, just need the VM name as a parameter to show where it is
placed.

Param($VMname)
 $Level=1
 cls
 Write-host "Searching for $VMname ...."

Try { $FolderName= Get-VM -name $VMName | Select Folder}
 Catch
 { Write-Host "Something went wrong. Are You connected to vCenter ?" -ForeGroundColor Red
 Break
 }
 $CurrentFolder=$FolderName.Folder
 If ($CurrentFolder.name -eq $NULL)
 {
 Write-Host "$VMName is wrong or might not exist"
 Exit
 }
 While ($CurrentFolder.name -ne "vm")
 {

Write-host "Level($Level) : $CurrentFolder"
 $Parent= Get-Folder $CurrentFolder | Select parent
 $Path=$CurrentFolder.name + "\" + $Path
 $CurrentFolder=$Parent.Parent
 if ($CurrentFolder.count -gt 0 )
 {$currentFolder= $CurrentFolder[0]
 }
 $Level=$Level + 1
 }
 cls
 Write-host "Final Path:" -ForeGroundColor "White" $Path

Advertisements

6 thoughts on “How to find a VM folder path with PowerCli ?”

  1. What parts of this script do I need to modify? When I copy and paste this exactly into a ps1 file and run this on PowerCLI 5.5 R1 it errors. I’m not familiar with scripting this stuff so any help would be great.

  2. Thanks for the script it has helped me in a bind. I did come across some issues I want to share. We have muliple datacentres and therefore duplicate folder names. May I recommend you use the id and parentID to identify your folders.

    While ($CurrentFolder.Parentid -ne $null)
    {
    Write-host “Level($Level) : $CurrentFolder”
    $Parent= Get-Folder -Id $CurrentFolder.id | Select parent
    $Path=$CurrentFolder.name + “\” + $Path
    $CurrentFolder=$Parent.Parent
    if ($CurrentFolder.count -gt 0 )
    {$currentFolder= $CurrentFolder[0]
    }
    $Level=$Level + 1
    }
    Write-host “Final Path:” $Path
    $ObjCurrent.Path = $Path
    $DCCollection += $ObjCurrent
    }

    Thanks again.

      1. Hi!!

        I have pasted together the 2 versions of the script:

        Param($VMname)
        $Level=1
        cls
        Write-host “Searching for $VMname ….”

        Try { $FolderName= Get-VM -name $VMName | Select Folder}
        Catch
        { Write-Host “Something went wrong. Are You connected to vCenter ?” -ForeGroundColor Red
        Break
        }
        $CurrentFolder=$FolderName.Folder
        If ($CurrentFolder.name -eq $NULL)
        {
        Write-Host “$VMName is wrong or might not exist”
        Exit
        }
        While ($CurrentFolder.Parentid -ne $null)
        {
        Write-host “Level($Level) : $CurrentFolder”
        $Parent= Get-Folder -Id $CurrentFolder.id | Select parent
        $Path=$CurrentFolder.name + “\” + $Path
        $CurrentFolder=$Parent.Parent
        if ($CurrentFolder.count -gt 0 )
        {$currentFolder= $CurrentFolder[0]
        }
        $Level=$Level + 1
        }
        Write-host “Final Path:” $Path
        $ObjCurrent.Path = $Path
        $DCCollection += $ObjCurrent

        And it is working fine, but in the end it shows an error:

        Searching for oprmexprd1 ….
        Level(1) : AD
        Level(2) : Infrastructure
        Level(3) : Production
        Level(4) : vm
        Final Path: vm\Production\Infrastructure\AD\

        Property ‘Path’ cannot be found on this object; make sure it exists and is sett
        able.
        At D:\_opera\josaraho\get-VM-folder.ps1:29 char:13
        + $ObjCurrent. <<<< Path = $Path
        + CategoryInfo : InvalidOperation: (Path:String) [], RuntimeExcep
        tion
        + FullyQualifiedErrorId : PropertyNotFound

        Is this error expected? Is there a way to avoid it?

        Thank you very much for share your knowledge!

  3. Hi José !

    I think we could just remove these last lines to prevent that error message.
    You kept me reflecting this script and even though I haven’t got the correct scenario now, a vCenter reference would be great on Final Path. So it could be like this:

    Param($VMname)
    $Level=1
    cls
    Write-host “Searching for $VMname ….”

    Try { $FolderName= Get-VM -name $VMName}
    Catch
    { Write-Host “Something went wrong. Are You connected to vCenter ?” -ForeGroundColor Red
    Break
    }
    $CurrentFolder=$FolderName.Folder
    $vC= $FolderName.client.serveruri
    If ($CurrentFolder.name -eq $NULL)
    {
    Write-Host “$VMName is wrong or might not exist”
    Exit
    }
    While ($CurrentFolder.Parentid -ne $null)
    {
    Write-host “Level($Level) : $CurrentFolder”
    $Parent= Get-Folder -Id $CurrentFolder.id | Select parent
    $Path=$CurrentFolder.name + “\” + $Path
    $CurrentFolder=$Parent.Parent
    if ($CurrentFolder.count -gt 0 )
    {$currentFolder= $CurrentFolder[0]
    }
    $Level=$Level + 1
    }
    Write-host “Final Path on $vC :” $Path

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s