PowerCLI

 View Only
Expand all | Collapse all

Average Memory Memory utilisation report

  • 1.  Average Memory Memory utilisation report

    Posted Dec 15, 2021 06:17 AM
      |   view attached

    Afternoon.  I have a client that has a small but heavily used environment.

    2 x clusters with 2 x hosts in each cluster.  They have now unable to have a failure as they are consuming too much memory to allow us to perform patching as we can not put a host into maintenance as there are now not enough memory to support 1 host out of the cluster.

    I am trying to get a report out that shows the average memory utilisation of each VM over 30 days.

    The script is below.

     #Editable Varialbles.

    $vCenterServerName = "removed"

    $User = "removed"

    $Password = "removed"

    $Export = "removed"

    $From = "removed"

    $To = "removed"

    $Subject = "Average Capacity Report"

    $emailbody = "Please find attached the Average Capacity Report"

    $SMTPserver = "removed"

    #Non-Editable Variables.

    $Attachment = $Export

    #Virtual Centre Connection.

    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

    Connect-VIServer $vCenterServerName -AllLinked -User $User -Password $Password

    #Collection Virtual Server Information.

    Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, Host, NumCpu, MemoryMB, `

    @{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}}, `

    @{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}} , `

    @{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}} , `

    @{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}} |`

    #Update export file location to be relevant to your network.

    Export-Csv $Export

    #Send Email Message.

    Send-Mailmessage -From $From -To $To -Subject $Subject -Attachments $Attachment $emailbody -Priority Normal -SmtpServer $SMTPServer

    Disconnect-VIServer $vCenterServerName -Force -confirm:$false 

     

    When the script runs and emails it produces a csv and emails it which is what I am after but the output has columns in it below.  I am able to populate all fields except the Host field. What do I need to add to my script to populate the Host field.  I thought I could pull it from the Host area when looking at the summary of the VM.

    NameHostNumCpuMemoryMBCPU Usage (Average), MhzMemory Usage (Average), %Network Usage (Average), KBps

    Disk Usage (Average), KBps

    Attachment(s)

    csv
    report.csv   1 KB 1 version


  • 2.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 08:48 AM

    On the Select line, replace the Host entry with the calculated property

    @{N='Host';E={$_.VMHost.Name}}

     



  • 3.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 09:24 PM

    Thank you very much. appreciate your assistance.

    I edited the following line -

     Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, Host, NumCpu, MemoryMB, ` 

    To be

     Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, @{N='Host';E={$_.VMHost.Name}}, NumCpu, MemoryMB, ` 

     

    Is there a way to add in the cluster name?  Could I add in @{N='Cluster';E={$_.Cluster.Name}}

     



  • 4.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 09:41 PM

    No, but you could add

    @{N='Cluster';E={(Get-Cluster -VM $_).Name}}


  • 5.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 09:58 PM

    Thank you kind sir.

     

    Much appreciative for the assistance.

     



  • 6.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 10:08 PM

    Last one.

    Looking at getting the active memory utilisation of the VM.

     

    So if assigned 20GB of memory but actually using 1.8GB, what would I require.

     

    Thanks Again.



  • 7.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 10:11 PM

    Isn't that what 'Memory Usage (Average), %' is already showing you?

    Another option is looking at the Host memory used.

    @{N='Memory Used MB';E={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}}


  • 8.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 10:28 PM
      |   view attached

    Hi Lucid.

    Maybe I was incorrect in my explanation.

    I want to see the memory usage of the VM. not the % in ths case the the % is a average over the days of collection..

    So if they have 20GB assigned but are at this time consuming 1.8GB of memory on the server, can I show that as per the screen capture.

    If not that is fine.

     



  • 9.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 10:38 PM

    Did you try the calculated property as well?



  • 10.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 10:46 PM

    Yes that gave me what was assigned to the VM not was in use by the VM.

    I am not script writer, I google for them and try to make them work, so I might be wrong in my question.

    I see utilisation of 20GB but 1.8GB memory Active for a VM.



  • 11.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 11:11 PM

    That would be

    @{N='Memory Used MB';E={$_.ExtensionData.Summary.QuickStats.ActiveMemory}}


  • 12.  RE: Average Memory Memory utilisation report

    Posted Dec 15, 2021 11:23 PM

    Thanks for that, but unfortunately field is blank.

     

    NameClusterHostNumCpuMemoryMBCurrent Memory Used, MBMemory Usage (Average), %CPU Usage (Average), MhzNetwork Usage (Average), KBpsDisk Usage (Average), KBps
    removedremovedremoved420480 7.52428.5364.83341.27
    removedremovedremoved416384 1.3234.680.053.59

     

    they would hopefully read the 2200 for the first one and  163 as MB for Current Memory Used in MB.

    Script - #Virtual Centre Connection.

    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

    Connect-VIServer $vCenterServerName -AllLinked -User $User -Password $Password

    #Collection Virtual Server Information.

    Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, @{N='Cluster';E={(Get-Cluster -VM $_).Name}}, @{N='Host';E={$_.VMHost.Name}}, NumCpu, MemoryMB, `

    @{N="Current Memory Used, MB";E={$_.ExtensionData.Summary.QuickStats.MemoryActive}}, ` @{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}}, `

    @{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}}, `

    @{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}}, `

    @{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average),2)}} |`

    #Update export file location to be relevant to your network.

    Export-Csv $Export

    #Send Email Message.

    Send-Mailmessage -From $From -To $To -Subject $Subject -Attachments $Attachment $emailbody -Priority Normal -SmtpServer $SMTPServer

    Disconnect-VIServer $vCenterServerName -Force -confirm:$false



  • 13.  RE: Average Memory Memory utilisation report

    Posted Dec 16, 2021 08:51 AM

    That is ActiveMemory, not MemoryActive.


    PS1: you don't need those back-ticks at the end of the lines
    PS2: if your PowerCLI installation is > 6.5R1, you don't need the Add-PSSnapin anymore

    Connect-VIServer $vCenterServerName -AllLinked -User $User -Password $Password
    
    #Collection Virtual Server Information.
    
    Get-VM | Where-Object { $_.PowerState -eq "PoweredOn" } |
    Select-Object Name, @{N = 'Cluster'; E = { (Get-Cluster -VM $_).Name } }, @{N = 'Host'; E = { $_.VMHost.Name } }, NumCpu, MemoryMB,
    @{N = "Current Memory Used, MB"; E = { $_.ExtensionData.Summary.QuickStats.ActiveMemory } },
    @{N = "Memory Usage (Average), %" ; E = { [Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average), 2) } },
    @{N = "CPU Usage (Average), Mhz" ; E = { [Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average), 2) } },
    @{N = "Network Usage (Average), KBps" ; E = { [Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average), 2) } },
    @{N = "Disk Usage (Average), KBps" ; E = { [Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 60 | Measure-Object Value -Average).Average), 2) } } |
    
    #Update export file location to be relevant to your network.
    Export-Csv $Export
    #Send Email Message.
    Send-MailMessage -From $From -To $To -Subject $Subject -Attachments $Attachment $emailbody -Priority Normal -SmtpServer $SMTPServer
    
    Disconnect-VIServer $vCenterServerName -Force -Confirm:$false