Hi guys,
I need to get a report that will look into a vCenter's VM folders and will report back the total amount of RAM used by each folder (provisioned and active ideally), CPU used (MHz) and maybe disk space used
We organize VMs by product/purpose, our idea is that by looking at their folders we are able to estimate total amount of resource usage per product. I'd like to be able to schedule this as a weekly task and get the result sent in HTML form via email.
I've tried piecing together code snippets that I've found but none seem to do the trick.
Any help would be greatly appreciated.
Like this
Get-VM | Group-Object -Property {$_.Folder.Id} |
Select @{N="Folder";E={
$folder = $_.Group[0].Folder
$path = $folder.Name
while($folder.Parent){
$folder = $folder.Parent
$path = $folder.Name + "/" + $path
}
$path
}},
@{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
@{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}} |
Export-Csv C:\report.csv -NoTypeInformation -use
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Where are you stuck?
To get your report you could: Get-VM -Location FolderName | FT Name, *CPU*, MemoryGB, *space*
So I'm using this:
Get-VM -location "folder" | Select-Object Name, NumCPU, MemoryGB | Export-Csv "C:\CPU_Memory.csv" -NoTypeInformation
How can I get the script to calculate the totals for the NumCpu and MemoryGB and add them to the csv ?
Also, this works per folder, how can I modify this to work per vCenter. What I'd ideally end up with is a script that looks into each folder and reports on the total NumCPU and MemoryGB values.
Thanks!
The following PowerCLI script will report the total number of virtual CPU's and MemoryGB per VM folder:
Get-Folder -Type VM |
ForEach-Object {
$Folder = $_
$VMs = $Folder | Get-VM
$Folder |
Select-Object -Property Name,
@{N="TotalNumCpu";E={$VMs | Measure-Object -Property NumCpu -Sum | Select-Object -ExpandProperty Sum}},
@{N="TotalMemoryGB";E={$VMs | Measure-Object -Property MemoryGB -Sum | Select-Object -ExpandProperty Sum}}
}
This will give you the list per vCenter, provided you are not running in multiple mode, with the full folderpath (which can be handy in case of duplciate names).
And it uses only 1 Get-VM call
Get-VM | Group-Object -Property {$_.Folder.Id} |
Select @{N="Folder";E={
$folder = $_.Group[0].Folder
$path = $folder.Name
while($folder.Parent){
$folder = $folder.Parent
$path = $folder.Name + "/" + $path
}
$path
}},
@{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
@{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you both, this is exactly what I was looking for!
Last question - how can I format the output into an html file or csv that I can mail after the script runs?
You can pipe the output to the Export-CSV cmdlet to generate a .csv file or to the ConvertTo-Html cmdlet to generate a html file.
So I've added this to the end of the script:
ConvertTo-HTML | Out-File "C:\Users\test.html"
I see the right output on the console, but the html file is empty:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
</table>
</body></html>
I'm sure I'm missing something
Like this
Get-VM | Group-Object -Property {$_.Folder.Id} |
Select @{N="Folder";E={
$folder = $_.Group[0].Folder
$path = $folder.Name
while($folder.Parent){
$folder = $folder.Parent
$path = $folder.Name + "/" + $path
}
$path
}},
@{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
@{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}} |
Export-Csv C:\report.csv -NoTypeInformation -use
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That will not work with a Foreach loop like that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Right on the money!! Thank you very much.
Hi,
I want to add a column for the total number of VMs per folder, I've added this:
@{N="Folder";E={$_.Name}},@{N="Number of VM's";E={($_|Get-VM|Measure-Object).Count}},
--------------
Get-VM | Group-Object -Property {$_.Folder.Id} |
Select @{N="Folder";E={
$folder = $_.Group[0].Folder
$path = $folder.Name
while($folder.Parent){
$folder = $folder.Parent
$path = $folder.Name + "/" + $path
}
$path
}},
@{N="Folder";E={$_.Name}},@{N="Number of VM's";E={($_|Get-VM|Measure-Object).Count}},
@{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
@{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}} |
Export-Csv C:\report.csv -NoTypeInformation -use
It fails to run, what am I missing ?
Try like this
Get-VM | Group-Object -Property {$_.Folder.Id} |
Select @{N="Folder";E={
$folder = $_.Group[0].Folder
$path = $folder.Name
while($folder.Parent){
$folder = $folder.Parent
$path = $folder.Name + "/" + $path
}
$path
}},
@{N="NumCPU";E={$_.Group | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum}},
@{N="MemoryGB";E={$_.Group | Measure-Object -Property MemoryGB -Sum | Select -ExpandProperty Sum}},
@{N="Number of VM's";E={$_.Group.Count}} |
Export-Csv C:\report.csv -NoTypeInformation -use
The Group property in the objects returned by the Group-Object cmdlet, holds all the VMs in a specific folder.
So we just need to count the number of entries.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks again!
I added a row to get ProvisionedSpaceGB, and it works good:
@{N="ProvisionedSpaceGB";E={$_.Group | Measure-Object -Property ProvisionedSpaceGB -Sum | Select -ExpandProperty Sum}},
But it comes out with a lot of decimals (6 I think).
I was reading this PowerCLI 4.1 brings the New-VIProperty cmdlet | LucD notes
I tried doing this:
@{N="ProvisionedSpaceGB";E={"{0:f1}"{$_.Group | Measure-Object -Property ProvisionedSpaceGB -Sum | Select -ExpandProperty Sum}},
Try it like this
@{N="ProvisionedSpaceGB";E={"{0:f1}" -f ($_.Group | Measure-Object -Property ProvisionedSpaceGB -Sum | Select -ExpandProperty Sum)}},
You forgot to add the format operator.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference