VMware Cloud Community
fcocquyt16
Contributor
Contributor

Adding fields to this report

Given this awesome script from PowerCLI: Get every VM added to vCenter in the last 30 Days! - The Practical Administrator

I modified it to output to a CSV, now I'd like to add fields for CPU, memory, disk allocated- how is that best accomplished?


thanks!


#requires -pssnapin VMware.Vimautomation.Core

Param(

    [int]$LastDays

)

Process

{

    $EventFilterSpecByTime = New-Object VMware.Vim.EventFilterSpecByTime

    If ($LastDays)

    {

        $EventFilterSpecByTime.BeginTime = (get-date).AddDays(-$($LastDays))

    }

    $EventFilterSpec = New-Object VMware.Vim.EventFilterSpec

    $EventFilterSpec.Time = $EventFilterSpecByTime

    $EventFilterSpec.DisableFullMessage = $False

    $EventFilterSpec.Type = "VmCreatedEvent","VMNumCpu","VmDeployedEvent","VmClonedEvent","VmDiscoveredEvent","VmRegisteredEvent"

    $EventManager = Get-View EventManager

    $NewVmTasks = $EventManager.QueryEvents($EventFilterSpec)

    Foreach ($Task in $NewVmTasks)

    {

        # If VM was deployed from a template then record which template.

        If ($Task.Template -and ($Task.SrcTemplate.Vm))

        {

           

            $srcTemplate = (Get-View $Task.SrcTemplate.Vm -Property name).Name

        }

        Else

        {

            $srcTemplate = $null

        }

        write-output ""|Select-Object @{

                Name="Name"

                Expression={$Task.Vm.name}

            }, @{

  Name="CPU"

  Expression={$Task.Vm.NumCpu}

  }, @{

                Name="Created"

                Expression={$Task.CreatedTime}

            }, @{

                Name="UserName"

                Expression={$Task.UserName}

            }, @{       

                Name="Type"

                Expression={$Task.gettype().name}

            }, @{

                Name="Template"

                Expression={$srcTemplate}

            } | export-csv -append SAN30dayreport.csv

    }

}

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership

You are looking at Events, in such an event there is a MoRef (pointer) to a VM.

See for example the VmClonedEVent, this object inherits properties from a number of other objects.

If you drill down to the Event, you will notice that this object has a property Vm.Vm.

In there you will find the MoRef. With Get-View you can get the VirtualMachine object to which this MoRef is pointing.

And in the object, you will find the Config.Hardware.NumCpu property.

A possible script could look something like this

Note that this kind of scrips require a certain knowledge of the vSphere SDK.

Param(

    [int]$LastDays

)

Process

{

    $report = @()

    $EventFilterSpecByTime = New-Object VMware.Vim.EventFilterSpecByTime

    If ($LastDays)

    {

        $EventFilterSpecByTime.BeginTime = (get-date).AddDays(-$($LastDays))

    }

    $EventFilterSpec = New-Object VMware.Vim.EventFilterSpec

    $EventFilterSpec.Time = $EventFilterSpecByTime

    $EventFilterSpec.DisableFullMessage = $False

    $EventFilterSpec.Type = "VmCreatedEvent","VmDeployedEvent","VmClonedEvent","VmDiscoveredEvent","VmRegisteredEvent"

    $EventManager = Get-View EventManager

    $NewVmTasks = $EventManager.QueryEvents($EventFilterSpec)

    Foreach ($Task in $NewVmTasks)

    {

        # If VM was deployed from a template then record which template.

        If ($Task.Template -and ($Task.SrcTemplate.Vm))

        {

          

            $srcTemplate = (Get-View $Task.SrcTemplate.Vm -Property name).Name

        }

        Else

        {

            $srcTemplate = $null

        }

        $numcpu = $null

        if($Task.VM.VM){

            $vm = Get-View -Id $Task.VM.VM -ErrorAction SilentlyContinue

            if($vm){

                $numcpu = $vm.Config.Hardware.NumCpu

            }

        }

        $report += '' | Select-Object @{

                Name="Name"

                Expression={$Task.Vm.name}

            },  @{

                Name="Created"

                Expression={$Task.CreatedTime}

            }, @{

                Name="UserName"

                Expression={$Task.UserName}

            }, @{      

                Name="Type"

                Expression={$Task.gettype().name}

            }, @{

                Name="Template"

                Expression={$srcTemplate}

            }, @{

                Name="NumCPU"

                Expression={$numcpu}

            }

    }

    $report | Export-Csv report.csv -NoTypeInformation -UseCulture

}


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

Reply
0 Kudos
fcocquyt16
Contributor
Contributor

Tried your code - results in an empty report.csv Smiley Sad

Reply
0 Kudos
LucD
Leadership
Leadership

Sorry, my bad, typo.
I forgot to pipe a dummy into the Select, it's corrected now.

Please try again.


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

Reply
0 Kudos
fcocquyt16
Contributor
Contributor

Great - CPU count is returned - how can I add memory (memoryMB returns null) and disk?:

           }, @{

                Name="NumCPU"

                Expression={$numcpu}

  }, @{

                Name="MemoryMB"

                Expression={$memorymb}

# }, @{

#                Name="Disk"

#          @{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )}

thanks Luc

Reply
0 Kudos
LucD
Leadership
Leadership

You have access to the VirtualMachine object in $vm, so it's a matter of picking the properties.

Something like this.

Param(

    [int]$LastDays

)

Process

{

    $report = @()

    $EventFilterSpecByTime = New-Object VMware.Vim.EventFilterSpecByTime

    If ($LastDays)

    {

        $EventFilterSpecByTime.BeginTime = (get-date).AddDays(-$($LastDays))

    }

    $EventFilterSpec = New-Object VMware.Vim.EventFilterSpec

    $EventFilterSpec.Time = $EventFilterSpecByTime

    $EventFilterSpec.DisableFullMessage = $False

    $EventFilterSpec.Type = "VmCreatedEvent","VmDeployedEvent","VmClonedEvent","VmDiscoveredEvent","VmRegisteredEvent"

    $EventManager = Get-View EventManager

    $NewVmTasks = $EventManager.QueryEvents($EventFilterSpec)

    Foreach ($Task in $NewVmTasks)

    {

        # If VM was deployed from a template then record which template.

        If ($Task.Template -and ($Task.SrcTemplate.Vm))

        {

         

            $srcTemplate = (Get-View $Task.SrcTemplate.Vm -Property name).Name

        }

        Else

        {

            $srcTemplate = $null

        }

        $numcpu = $null

        if($Task.VM.VM){

            $vm = Get-View -Id $Task.VM.VM -ErrorAction SilentlyContinue

            if($vm){

                $numcpu = $vm.Config.Hardware.NumCpu

                $mem = $vm.Config.Hardware.MemoryMB

                $dssum = $vm.Summary.Storage.Committed

            }

        }

        $report += '' | Select-Object @{

                Name="Name"

                Expression={$Task.Vm.name}

            },  @{

                Name="Created"

                Expression={$Task.CreatedTime}

            }, @{

                Name="UserName"

                Expression={$Task.UserName}

            }, @{     

                Name="Type"

                Expression={$Task.gettype().name}

            }, @{

                Name="Template"

                Expression={$srcTemplate}

            }, @{

                Name="NumCPU"

                Expression={$numcpu}

            }, @{

                Name="MemoryMB"

                Expression={$mem}

            }, @{

                Name="StorageCommitted"

                Expression={$dssum}

            }

    }

    $report | Export-Csv report.csv -NoTypeInformation -UseCulture

}


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

Reply
0 Kudos
fcocquyt16
Contributor
Contributor

Can the $dssum be better presented with  @{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )} ?

Prime movitation for this report is funding more hardware to grow clusters - showing allocated disk space vs thin/used is better chargeback

thanks

Reply
0 Kudos
LucD
Leadership
Leadership

You're dealing with a vSphere VirtualMachine object (obtained through the MoRef in the event), not a .Net VirtualMachine object (returned by Get-VM).

Under the cover the PowerCLI cmdlet is getting the values from the vSphere objects as well, but it will create a new .Net object, in which a number of selected properties are present.

Btw, do you see different values for space used from both objects?


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

Reply
0 Kudos
fcocquyt16
Contributor
Contributor

Yes - the space reported by

@{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )}} is human readable - allocated disk space in GB

The $vm.Summary.Storage.Committed does not seem to match allocated disk at all - and its in 10^20 bits Smiley Happy

We can't report from the two context's you mentioned in the same script?

thanks

Reply
0 Kudos
LucD
Leadership
Leadership

You can divide by the constant 1GB and do a [math]::round


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

Reply
0 Kudos