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.
Name | Host | NumCpu | MemoryMB | CPU Usage (Average), Mhz | Memory Usage (Average), % | Network Usage (Average), KBps | Disk Usage (Average), KBps |
On the Select line, replace the Host entry with the calculated property
@{N='Host';E={$_.VMHost.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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}}
No, but you could add
@{N='Cluster';E={(Get-Cluster -VM $_).Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you kind sir.
Much appreciative for the assistance.
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.
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}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
Did you try the calculated property as well?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
That would be
@{N='Memory Used MB';E={$_.ExtensionData.Summary.QuickStats.ActiveMemory}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for that, but unfortunately field is blank.
Name | Cluster | Host | NumCpu | MemoryMB | Current Memory Used, MB | Memory Usage (Average), % | CPU Usage (Average), Mhz | Network Usage (Average), KBps | Disk Usage (Average), KBps |
removed | removed | removed | 4 | 20480 | 7.52 | 428.53 | 64.83 | 341.27 | |
removed | removed | removed | 4 | 16384 | 1.32 | 34.68 | 0.05 | 3.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
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference