How to Use Resource Metering With PowerShell

How to Use Resource Metering With PowerShell

  • Comments 5
  • Likes

Hey, it's Lalithra again. In the last part, we talked about what resource metering is. Now, we’ll get into how to use it with PowerShell.

A quick note: if this is your first foray into PowerShell, here are some resources you may find helpful to go through. First, a couple of posts from the Scripting Guy to give you a good overview: An Introduction and PowerShell and Pipelining. Also, check out the Windows PowerShell for the Busy Admin series of webcasts by Scripting Guy Ed Wilson. Highly recommended.

Let’s get started. First, let’s find the cmdlets we’re looking for.

PS C:\> Get-Command *VM*

This prints out a list of cmdlets with VM in their names. If we look through this list for something related to resource metering, we’ll find the cmdlets we’re looking for.

PS C:\> Get-Command *VMResourceMetering*

If you want more details on a particular cmdlet, use Get-Help.

Now, let’s enable resource metering.

PS C:\> Enable-VMResourceMetering –VMName Greendale-VM

We can see if a virtual machine has resource metering enabled by looking at its properties:

PS C:\> Get-VM –VMName Greendale | Format-Table Name, ResourceMeteringEnabled

Name                                                                        ResourceMeteringEnabled
----                                                                        -----------------------
Greendale-VM                                                                                   True

Here, I’m piping the VM Object into the Format-Table cmdlet, which will print out a table with the name of the virtual machine and whether resource metering is enabled or not. In this case, I see that metering is enabled.

If you want to disable metering, it’s simple. Just swap out the verb:

PS C:\> Disable-VMResourceMetering –VMName Greendale-VM

Let’s see what the resource utilization is like so far.

PS C:\> Measure-VM –VMName Greendale-VM

VMName       AvgCPU(MHz) AvgRAM(M) MaxRAM(M) MinRAM(M) TotalDisk(M) NetworkInbound(M) NetworkOutbound(M)
------       ----------- --------- --------- --------- ------------ ----------------- ------------------
Greendale-VM 86          1750      2608      1119      40960        9930              2945

Now I can see the CPU, memory, disk, and network utilization, as covered in the last post.

If I want to see the utilization in greater detail…

PS C:\> $report = Measure-VM –VMName Greendale-VM
PS C:\> $report | Format-List
AvgCPU                      : 86
AvgRAM                      : 1750
MinRAM                      : 1119
MaxRAM                      : 2608
TotalDisk                   : 40960
ComputerName                : LALITHRF-N1
VMId                        : 2aeec321-08e2-47c6-a360-dfafecff4525
VMName                      : VMM 2012 SP1
MeteringDuration            : 19.03:57:44.2660000
AverageProcessorUsage       : 86
AverageMemoryUsage          : 1750
MaximumMemoryUsage          : 2608
MinimumMemoryUsage          : 1119
TotalDiskAllocation         : 40960
NetworkMeteredTrafficReport : {Microsoft.HyperV.PowerShell.VMNetworkAdapterPortAclMeteringReport,
                              Microsoft.HyperV.PowerShell.VMNetworkAdapterPortAclMeteringReport,
                              Microsoft.HyperV.PowerShell.VMNetworkAdapterPortAclMeteringReport,
                              Microsoft.HyperV.PowerShell.VMNetworkAdapterPortAclMeteringReport}

Here, I’m storing the output of Measure-VM into a variable, $report. I’m printing out everything in this report in list form.

A quick aside, you may have noticed that we have seemingly redundant properties such as AvgCPU and AverageProcessorUsage. We use the shorter parameter names for the column headings in our tables, as seen above. This allows the table to fit on a PowerShell prompt. Since it is likely that you would use the names you see in the tables to access the various properties, we added the shorter names as aliases, so that the properties remain intuitive to work with.

If I want more details on the network utilization, I’ll want to look at the NetworkMeteredTrafficReport property:

PS C:\> $report.NetworkMeteredTrafficReport

LocalAddress RemoteAddress Direction TotalTraffic(M)
------------ ------------- --------- ---------------
             *.*           Outbound  308
             *:*           Inbound   6047
             *.*           Inbound   3883
             *:*           Outbound  2637

What if I want to add another ACL? As we talked about on the last post, ACLs, set on a virtual machine’s network adapter, allow you measure traffic that uses your internet versus your intranet, or any other division you choose.

PS C:\> Add-VMNetworkAdapterAcl –VMName Greendale-VM –Action Meter –Direction Inbound –RemoteIpAddress 10.0.0.0/8

If I want to remove it, simply swap the verb:

PS C:\> Remove-VMNetworkAdapterAcl –VMName Greendale-VM –Action Meter –Direction Inbound –RemoteIpAddress 10.0.0.0/8

One side note: Disable-VMResourceMetering does not remove the default *.* and *:* metering ACLs. If you want to remove them, use the Remove-VMNetworkAdapterAcl cmdlet. If you want to use SR-IOV, you will want to remove them, as metering and SR-IOV are not compatible.

Finally, now that I’ve measured the data I’m interested in, and saved it for your chargeback solution, I need to reset resource metering, so that it collects the data just for this next billing cycle.

PS C:\> Reset-VMResourceMetering –VMName Greendale-VM

Now, if I measure again, I see that the values are reset:

PS C:\> Measure-VM –VMName Greendale-VM

VMName       AvgCPU(MHz) AvgRAM(M) MaxRAM(M) MinRAM(M) TotalDisk(M) NetworkInbound(M) NetworkOutbound(M)
------       ----------- --------- --------- --------- ------------ ----------------- ------------------
Greendale-VM 1557        1198      1198      1198      40960        2                 1

That’s it! You should be able to put those pieces together for your own purposes. Please let us know how you’re using it!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Great job!!! i will love it when server 2012  is released :D

  • great article, thank you so much.

    one simple question - would it be possible to somehow limit report, for example to check the utilization between certain dates? lets say that resource metering has been working for last 6 months, what if I wanted to check utilization for particular month only?

  • Hey Gale,

    To get resource utilization on a month by month basis, you'd need to measure every month. If you record your measurement at the granularity you need, you can always add it up to get the values over a six-month period. Then, you would also be able to view the data for a particular month.

    Thanks,

    L

  • @Lalithra

    Thanks. If I got it correct, am I supposed to record values at the end of each month and then reset stats?

    That seems like a lot of manual work, I am sure that you could have designed this feature to be simpler for billing purposes...

  • @Gale,

         This “Resource Counter” approach is consistent with SQL Server, Perfmon Counters & many other resource tracking metrics. They provide the values, you decide the granularity, the storage mechanism & how much historical info you want to keep.  

    Personally if I had a business need for this information I’d capture the data much more frequently & move it to a more reliable store.

    It would be trivial to schedule a tiny PS script to run nightly (or even hourly). Then integrate it into your corporate monitoring & reporting system. eg: System Center, SQL’s Management Data Warehouse, Splunk, etc.

    Anything less than 1 hour & I’d probably just track if with Perfmon or Extended Events.