VMware Cloud Community
SaqiChangx
Enthusiast
Enthusiast
Jump to solution

List Vms Report Cluster and Folder

Hi everyone,

I need your suggestions. I have a script that reports me daily about new VMs and their details. now I want to extend this script and want to add 2 more Row (cluster and Folder). I tried myself but nothing came out.

these 2 rows should contain this info:

1: Folder name(if possible with path)

2: Cluster name.

my Script:

$report = [System.Collections.ArrayList]@()

$view = Get-View -ViewType Virtualmachine

$cnt = 1

Write-Host "Found $($view.count) virtual machines"

$events = Get-VIEvent -Entity $view.Name -MaxSamples ([int]::MaxValue) |

        Where-Object { $_ -is [VMware.Vim.VmDeployedEvent] -or

                      $_ -is [VMware.Vim.VmClonedEvent]-or

                      $_ -is [VMware.Vim.VmCreatedEvent]-or

                      $_ -is [VMware.Vim.VmPoweredOffEvent]-or

                      $_ -is [VMware.Vim.VmRegisteredEvent]}

Write-Host "Found $($view.count) virtual machines"

foreach($vm in $view ) {

    if (!$vm.Config) { continue }

Write-Host "Processing $($cnt) - $($vm.Name)"

    $vms = [PSCustomObject]@{

      VMName = $vm.Name

  Responsible_Team = ""

      Hostname = $vm.guest.hostname

      OS = $vm.Config.GuestFullName

      IPAddress = $vm.guest.ipAddress

      Boottime = $vm.Runtime.BootTime

# PoweredOff = ($events | where{$_.VM.Name -eq $vm.Name} | Sort-Object -Property CreatedTime -Descending | Select -First 1).CreatedTime -replace '(\d+:\d+):\d+'

      VMState = $vm.summary.runtime.powerState

      UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)

      ProvisionedSpaceGB = [math]::Round(($vm.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,2)

      MemoryUsage = $vm.summary.quickStats.guestMemoryUsage

      Datastore = $vm.Config.DatastoreUrl[0].Name

      CreatedDate = $vm.Config.CreateDate

      Unit = ""

      Notes = $vm.Config.Annotation -replace '(?:\r|\n)',''

      ToolsStatus = $vm.guest.toolsstatus

      ToolsVersion = $vm.config.tools.toolsversion

      Folder = ""

      Cluster = ""

    }

  

    foreach ($c in $vm.CustomValue) {

    switch ($c.Key) {

        "901" { $vms.Unit = $c.Value }

      }

    }

    $Report.Add($vms) | Out-Null

    $cnt++

}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

        Folder             = (Get-View -Id $vm.Parent -Property Name).Name

        Cluster            = &{

            $parent = Get-View -Id $vm.ResourcePool -Property Name,Parent

            while($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent.Parent -ne $null){

                $parent = Get-View -Id $parent.Parent -Property Name,Parent

            }

            if($parent -is [VMware.Vim.ClusterComputeResource]){

                $parent.Name

            }

        }


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

View solution in original post

7 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

        Folder             = (Get-View -Id $vm.Parent -Property Name).Name

        Cluster            = &{

            $parent = Get-View -Id $vm.ResourcePool -Property Name,Parent

            while($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent.Parent -ne $null){

                $parent = Get-View -Id $parent.Parent -Property Name,Parent

            }

            if($parent -is [VMware.Vim.ClusterComputeResource]){

                $parent.Name

            }

        }


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

SaqiChangx
Enthusiast
Enthusiast
Jump to solution

Hi, Thank you, sir, you are great, it works, but I forgot to mention that I also have templates. and the folder and cluster of Templates are null.

Error:

Cannnot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At C:\Users\Administrator\Downloads\Scripts\VmInventory.ps1:65 char:36 +             $parent = Get-View -Id $vm.ResourcePool -Property Name,Pa ... +                                    ~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException     + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

How i correct that.

Reply
0 Kudos
SaqiChangx
Enthusiast
Enthusiast
Jump to solution

Sorry just Cluster is null or Empty.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, change the Cluster entry to this

        Cluster            = & {

            if (-not $vm.Config.Template) {

                $parent = Get-View -Id $vm.ResourcePool -Property Name, Parent

                while ($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent.Parent -ne $null) {

                    $parent = Get-View -Id $parent.Parent -Property Name, Parent

                }

                if ($parent -is [VMware.Vim.ClusterComputeResource]) {

                    $parent.Name

                }

            }

        }


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

SaqiChangx
Enthusiast
Enthusiast
Jump to solution

With this command, we ignore only Error but still, Cluster Field of Template is empty :

The output is filtered:

pastedImage_8.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, then try this different approach.

        Cluster            = & {

                $parent = Get-View -Id $vm.Runtime.Host -Property Name,Parent

                while ($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent.Parent -ne $null) {

                    $parent = Get-View -Id $parent.Parent -Property Name, Parent

                }

                if ($parent -is [VMware.Vim.ClusterComputeResource]) {

                    $parent.Name

                }

            }


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

SaqiChangx
Enthusiast
Enthusiast
Jump to solution

Thank you Sir for support, it is solved.

Reply
0 Kudos