I have a function that I have been using to collect Min,Max,Avg for CPU and Memory. The function collects data from the last month and returns a single value for each item. After reading about the New-VIProperty I thought I would try to adapt the function into Cluster properties. I got the property working except it returns an array of values instead of a single value. I would like help tweaking the 1st property and then I can create the others. I am going to attach the working function and the non working property.
Working Function
Function Min_Max_Avg ($E, $C, $S) {
Get-Stat -Entity $E -Stat $C -Start $S |
Group-Object -Property {$_.Entity.Name} | %{
$stats = $_.Group | Measure-Object -Property Value -Average -Maximum -Minimum
}
$Avg = [math]::round(($stats.Average), 0)
$Min = [math]::round(($stats.Minimum), 0)
$Max = [math]::round(($stats.Maximum), 0)
Return $Avg,$Min,$Max }
Non working Property
#MemoryMinUsageMonth -MB
New-VIProperty -Name MemoryMinUsageMonth -ObjectType Cluster -Value{
param($cluster)
$start = (Get-Date).AddMonths(-1)
Get-Stat -Entity $cluster -Stat mem.usage.average -Start $start
Group-Object -Property {$cluster.Entity.Name} | %{
$stats = $cluster.Group | Measure-Object -Property Value -Minimum
}
$Min = [math]::round(($stats.Minimum), 0)
$Min
}
Afaik, the New-VIProperty you defined is called for each cluster individually.
So there is no need to use the Group-Object.
Can you perhaps try it like this
#MemoryMinUsageMonth -MB
New-VIProperty -Name MemoryMinUsageMonth -ObjectType Cluster -Value{
param($cluster)
Get-Stat -Entity $cluster -Stat mem.usage.average -Start (Get-Date).AddMonths(-1) |
Measure-Object -Property Value -Minimum |
Select -ExpandProperty Minimum
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Afaik, the New-VIProperty you defined is called for each cluster individually.
So there is no need to use the Group-Object.
Can you perhaps try it like this
#MemoryMinUsageMonth -MB
New-VIProperty -Name MemoryMinUsageMonth -ObjectType Cluster -Value{
param($cluster)
Get-Stat -Entity $cluster -Stat mem.usage.average -Start (Get-Date).AddMonths(-1) |
Measure-Object -Property Value -Minimum |
Select -ExpandProperty Minimum
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Works, Thank you. Can you explain the need for "Select -ExpandProperty Minimum"
I noticed on your blog post from December 18, 2001 that you asked people to forward their VI-Properties. I couldn't seem to find a follow-up to that post. Is there an updated property list somewhere.
Also, I started writing some properties for the datacenter such as # of vms, consolidation ratios, and other high level reporting stats. That works great for environments with just one datacenter but I am racking my brain on how to create global a global property so that I can report for multiple virtual centers. Would just like to know your thoughts on that.
Thanks again LucD
With the Select-Object cmdlet you can select a number of properties from an object.
The result will be a new object.
With the ExpandProperty you suppress this creation of an object, and you just get the value of the property.
That's the short explanation :smileygrin:
The New-VIProperty module has been asleep for a while, but you are right I should urgently publish an update.
You can do global properties but it will require a bit more work in the code.
From the VM you'll have the datacenter to which it belongs, and once you have that you can get the # of VMs in that datacenter.
Let me know which specific one you would like to see as an example, and I'll try to come up with something over the weekend.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think you didn't understand what I was saying. A little background, I am collecting data for high level capacity reporting so some of the information isn't so actionable but it is information that senior management likes to see. For example a VM to Host ratio at a host level may be important for troubleshootinn for techs but the CIO doesn't care about individual hosts and just wants to see the ratio for the whole environment. I'm trying to write code to satisfy both. Right now I just have a bunch of scripts and I am looking for a better way to organize so that I'm not just cutting and pasting from scripts every time I get a request for a new dataset. I thought making new properties was the way to go but maybe I need to make functions. Just looking for your thoughts on that.
Here are all the things I am collecting.
#VMs
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
#Hosts
Global
Per Virtaul Center
Per Data Center
Per Cluster
#Clusters
Global
Per Virtaul Center
Per Data Center
#Datacenters
Global
Per Virtaul Center
#Virtual Centers
Global
VM -Host Ratio
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
#Physical CPU
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
#Virtual CPU
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
Per VM
Virtual CPU to Physical CPU Ratio
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
VM to Physical CPU Ratio
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
#Physical Memory
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
#Memory Allocated
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
#Average VM (# cpu, Memory, Storage allocated, Storage in use)
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
#Storage Allocated (Calculated from vm data)Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
Per VM
#Storage in Use (Calculated from vm data)
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
Per VM
#Storage saved through thin provisioning (StorageAllocated - Storage in Use)
Global
Per Virtaul Center
Per Data Center
Per Cluster
Per Host
#Min, Max, Average CPU usage last 30 days
Per Cluster
Per Host
Per VM
#Min, Max, Average CPU usage last 30 days
Per Cluster
Per Host
Per VM
I don't immediately see any big problems to calculate any of these.
But another question, why would you do that with a VIProperty.
That would mean the value has to be calculated for each entity that you display.
Your Get-VM will become awfully slow I'm afraid.
How do you like this idea:
Your Get- cmdlets will not suffer, and it is still easy to fetch the values.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD
You are correct, calculating any of those is easy, and I already have writing all the code. I was just looking for the best way to organize the code for re-use so I could easily share it or develop new scripts with similar functionality.
Not sure what you mean by "store all the 'properties' as custom attributes on each entity"
Can you give me an example for reference?
Sure, the following will create a custom attribute that contains the number of VMs on a cluster.
$caName = "NumberOfVM"
Try {
$ca = Get-CustomAttribute -Name $caName -TargetType Cluster -ErrorAction Stop
}
Catch {
$ca = New-CustomAttribute -Name $caName -TargetType Cluster
}
Get-Cluster | %{
Set-Annotation -Entity $_ -CustomAttribute $ca `
-Value (Get-VM -Location $_ | Measure-Object | Select -ExpandProperty Count)
}
Get-Cluster | Select Name,
@{N="CA";E={Get-Annotation -Entity $_ -CustomAttribute $caName | Select -ExpandProperty Value}}
First the script fetches/creates the custom attribute.
Then it sets the value of the custom attribute for each cluster.
In the last line I added an example of how you can retrieve the value of the custom attribute.
That is not needed in your scheduled task of course :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference