Hi,
How to get back the use of the CPU and the ram of my VM on vcenter with a ps script?
Thanks
Best regards
snap
Do you want the current or average CPU and RAM usage on your vCenter VM or do you want it on all VM's?
I want the current average CPU and RAM usage for each VM's
Thanks you
Snap
Try something like this
Get-VM | Get-Stat -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 |
Group-Object -Property {$_.Entity.Name} |
Select @{N="VM";E={$_.Values[0]}},
@{N="Timestamp";E={$_.group[0].Timestamp}},
@{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}},
@{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks you I check this
Thanks
Snap
HI,
I have this message
"The metric counter "mem.usage.average" doesn't exist for entity"
Do you get that for VMs ? Or only some ?\
Are there any VMs that are powered off perhaps ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi !
This is my script
$stat = @()
foreach($vm in Get-VM){
Get-Stat -entity $vm -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 |
}
I wish export this in a csv ?
I use this
$stat | export-csv "d:\test.csv" -notypeinformation -useculture
but it's not work
You can do it this way
Get-VM | Get-Stat -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | Group-Object -Property {$_.Entity.Name} |
Select @{N="VM";E={$_.Values[0]}},
@{N="Timestamp";E={$_.group[0].Timestamp}},
@{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}},
@{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}} |
Export-Csv "d:\test.csv" -NoTypeInformation -UseCulture
Or if you prefer your method, you should do
$stat = @() foreach($vm in Get-VM){ $stat += Get-Stat -entity $vm -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1
} $stat | export-csv "d:\test.csv" -notypeinformation -useculture
Note that this will export all properties to the CSV file, not sure if you want all thos
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok thank you a lot !
I check this
Thank you !
It's Ok thank you LucD you're awesome
Hi Lucd,
I wish add this
Get-HardDisk -vm $vm |Select-Object capacitygb
How I do this ?
Try something like this
Get-VM | Get-Stat -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | Group-Object -Property {$_.Entity.Name} | Select @{N="VM";E={$_.Values[0]}}, @{N="Timestamp";E={$_.group[0].Timestamp}},
@{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}},
@{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}},
@{N="HD Capacity";E={$_.Group[0].Entity.HardDisks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum}} | Export-Csv "d:\test.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It's OK
Thanks you for youre support
Lucd,
I try use this
@{N="Disk Use" ;E={$_.Group[0].Entity.vm | Measure-Object -Property UsedSpaceGB -sum | Select -ExpandProperty sum}}
for add this command
get-vm -name $vm| Select-Object UsedspaceGB
But it's doesn't Work
Can you help me (again)
Thank you !
That property is not present on the VirtualMachine object.
Try is like this
Get-VM | Get-Stat -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | Group-Object -Property {$_.Entity.Name} | Select @{N="VM";E={$_.Values[0]}}, @{N="Timestamp";E={$_.group[0].Timestamp}},
@{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}},
@{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}},
@{N="HD Capacity (GB)";E={$_.Group[0].Entity.HardDisks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum}},
@{N="HD Uses (GB)";E={[math]::Round($_.Group[0].Entity.ExtensionData.Summary.Storage.Committed/1GB)}} |
Export-Csv "d:\test.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LuCD,
As you see it I begin in Powershell, can you explain me the script please ?
Thank for your support !
Btw, this is a faster version of the script
$vms = Get-VM
Get-Stat -Entity $vms -Stat "cpu.usage.average","mem.usage.average" -Realtime -MaxSamples 1 | Group-Object -Property {$_.Entity.Name} | Select @{N="VM";E={$_.Values[0]}}, @{N="Timestamp";E={$_.group[0].Timestamp}}, @{N="CPU";E={$_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value}}, @{N="Memory";E={$_.Group | where {$_.MetricId -eq "mem.usage.average"} | Select -ExpandProperty Value}}, @{N="HD Capacity (GB)";E={[math]::Round($_.Group[0].Entity.ProvisionedSpaceGB)}},
@{N="HD Uses (GB)";E={[math]::Round($_.Group[0].Entity.UsedSpaceGB)}} |
Export-Csv "d:\test.csv" -NoTypeInformation -UseCulture
Some notes
Collect all the VMs in an array, that way we only need 1 call to Get-Stat
Let me know if you have further questions concerning the script
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LuCD,
I finaly understand the scrip and I thank you again and again for your help!
I have an another question,
How I can do To convert the result of this
@{N="Memory Use(GB)" ;E={$_.Group | where {$_.MetricId -eq "mem.active.average"} | Select -ExpandProperty Value}},
in GB ?
Thank you !
The metric mem.active.average returns values in KB, so you will have to divide by 1MB to get GB.
Luckily PowerShell has builtin constants for these values.
So you can do
@{N="Memory Use(GB)";E={$_.Group | where {($_.MetricId -eq "mem.active.average"} | Select -ExpandProperty Value)/1MB}},
And that will probably produce a rather long number, so it would be better to round it.
Like this
@{N="Memory Use(GB)";E={$_.Group | where {[Math]::Round(($_.MetricId -eq "mem.active.average"} | Select -ExpandProperty Value)/1MB)}},
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference