max2001
Contributor
Contributor

Folder by Path as a VM property - for VMs in a vApp

Jump to solution

Hi

I have used LucD’s new-viproperty script to create a folder property for each VM in my inventory. It works perfectly fine for VMs but not for VMs in a vApp as those ones don’t have a “folder” property in powercli. they report a $null value.

Would be much appreciated if someone can help me modify this script so it creates a VM property for both VMs and VMs in a vApp?

http://www.lucd.info/2012/05/18/folder-by-path/

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

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.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

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Check if this new version works for you.

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.VApp){

            $blue = Get-ParentName $object.VApp.ExtensionData.ParentFolder

            $name = $object.VApp.Name

        }

        elseif($object -is [VMware.Vim.ManagedObjectReference]){

            $folder = Get-VIObjectByVIView -MORef $object

            $blue = Get-ParentName $folder.Parent

            $name = $folder.Name

        }

        elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){

            $blue = Get-ParentName $object.Parent

            $name = $object.Parent.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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

Check if this new version works for you.

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.VApp){

            $blue = Get-ParentName $object.VApp.ExtensionData.ParentFolder

            $name = $object.VApp.Name

        }

        elseif($object -is [VMware.Vim.ManagedObjectReference]){

            $folder = Get-VIObjectByVIView -MORef $object

            $blue = Get-ParentName $folder.Parent

            $name = $folder.Name

        }

        elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){

            $blue = Get-ParentName $object.Parent

            $name = $object.Parent.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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
max2001
Contributor
Contributor

that worked, thanks!!

0 Kudos