Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell 3.0 to reset Hyper-V Resource Metering.
Hey, Scripting Guy! Ok, I have this problem here at work. We have upgraded our servers running Windows Server 2008 R2 to Windows Server 2012. We are running Hyper-V, and we need to get a resource report each month to cross-charge other departments for the computing resources they use. Here is the problem. I read your article last week about resource metering, and I told my boss I had found the problem. He immediately said, “Well that may work this month, but what about next month.” I stood there looking like a deer in the headlights while he explained, “If we tell a department they use xyz resources this month, how do we get a fresh number for next month? Are we supposed to keep adding and subtracting and all that to come up with new numbers?” Please help me. I am the one who told my boss that I thought we could do what we need to do by using Windows Server 2012 and that we did not need to buy a new product to manage our resources. You see, my boss is like a wookiee, and I don’t want to make him mad.
Microsoft Scripting Guy, Ed Wilson, is here. Well, this morning things are cool. In fact, the entire house is a bit cool because outside it is freezing. I know, I live in the Deep South, and I should not complain about cold, because our cold is really quite warm, when compared to places like Nome, Alaska. But, hey, to me it feels cold.
Anyway, I am up this morning because I am making an “Industry Leadership” live meeting with a group of Microsoft PFEs in the UK. I am talking about the things I did to help to raise my profile in the community. It is kind of cool, and I am glad to help out. (I am also honored to be invited to speak to this group of way-cool and talented people.)
Anyway, JJ, you do not need to worry about upsetting your wookiee manager. In fact, after today’s article, you will need to worry about escaping a wookiee hug instead.
Exam note One of the objectives on the Exam 70-410: Installing and Configuring Windows Server 2012 is configure Resource Metering, and so if you are working on your new MCSE for Server Infrastructure, you will want to know this material. Of course, if you are running or thinking about running Hyper-V on Windows Server 2012, you will want to know this information as well. The Introduction to Resource Metering blog post written by Lalithra Fernando on the Virtualization Blog is an excellent place to start. Lalithra also wrote a blog post called How to Use Resource Metering with PowerShell, which is also a good introduction.
Use PowerShell to see how long Resource Metering runs
Note This is the third article in a series about Hyper-V and Windows PowerShell. In the first article, I talked about using PowerShell to Meter Resources on Hyper-V Windows Server 2012. In the second article, I discussed using PowerShell to Configure Hyper-V Resource Metering. This article is also the fifth post in a series I have written about the MCSE exam 70-410.
Resetting Hyper-V Resource Meter counters is actually pretty easy—after all, there is a dedicated Windows PowerShell cmdlet to use to accomplish the task. If you need to do this on a monthly basis, use the Task Scheduler to create a task to run the cmdlet. Set it for 1 minute after midnight on the first day of the month—if that is your billing cycle—and you are good to go.
To see how long it has been since the Resource Metering counters have been reset, I use the Get-VM cmdlet to return a VirtualMachine object, and I pipe the object to the Measure-VM cmdlet. I then select the MeteringDuration property. The command below illustrates this technique.
10:24 C:> get-vm -VMName c1 | measure-vm | select meter*
If I want a report for all of my virtual machines, I only need to change the VMName property value to a wildcard. Once I do this, I decide I need to add the VMName property to the output. The revised command is shown here.
10:40 C:> get-vm -VMName * | measure-vm | select vmname, meter*
Reset the Resource Metering counters
To reset the Resource Metering counters for a single virtual machine I use the Get-VM cmdlet and specify a single computer name. I pipe the resulting VirtualMachine object to the Reset-VMResourceMetering cmdlet. This command is shown here.
get-vm -VMName c1 | Reset-VMResourceMetering
Nothing returns from running the previous command, so I use the Get-VM cmdlet to return a VirtualMachine object, and I pipe it to the Measure-VM cmdlet to obtain the Resource Metering report for the virtual machine. I then select the MeteringDuration property to see if the counter has been reset. This command is shown here, and it tells me that the counters were reset 24 seconds ago.
10:42 C:> get-vm -VMName c1 | measure-vm | select meter*
To reset the Resource Metering counters for all virtual machines on the host I use a wildcard to obtain all virtual machines on the local computer. I pipe the returned VirtualMachine objects to the Reset-VMResourceMetering cmdlet. The command is shown here.
get-vm -VMName * | Reset-VMResourceMetering
As before, nothing returns from running the Reset-VMResourceMetering cmdlet. To verify the changes took effect, I use the UP arrow and retrieve my previous Get-VM | Measure-VM | Select Meter* command. This time, I add the VMName to the output so I can see both virtual machine name as well as the metering duration.
10:43 C:> get-vm -VMName * | measure-vm | select vmname, meter*
JJ, that is all there is to using Windows PowerShell to reset Hyper-V Resource Metering counters. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy