{"id":4245,"date":"2013-01-28T00:01:00","date_gmt":"2013-01-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/01\/28\/use-powershell-to-configure-hyper-v-resource-metering\/"},"modified":"2013-01-28T00:01:00","modified_gmt":"2013-01-28T00:01:00","slug":"use-powershell-to-configure-hyper-v-resource-metering","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-configure-hyper-v-resource-metering\/","title":{"rendered":"Use PowerShell to Configure Hyper-V Resource Metering"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary:<\/strong><span style=\"font-size: 12px\"> Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell&nbsp;3.0 to configure Hyper-V Resource Metering on Windows Server&nbsp;2012.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well, today is Monday, and the first thing I do on Monday is update the Help on Windows PowerShell&nbsp;3.0. I use the command that is shown here.<\/p>\n<p style=\"padding-left: 30px\">Update-Help -Module * -Force<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;I need to open the Windows PowerShell console with admin rights to update Help.<\/p>\n<p>Once I have updated Help, I am prepared to start the week.<\/p>\n<p style=\"padding-left: 30px\"><strong>Exam note<\/strong> &nbsp;&nbsp;One of the objectives on the <a href=\"http:\/\/www.microsoft.com\/learning\/en\/us\/exam.aspx?ID=70-410\" target=\"_blank\">Exam 70-410: Installing and Configuring Windows Server 2012<\/a><em> <\/em>is <em>configure Resource Metering, <\/em>and so if you are working on your new <a href=\"http:\/\/www.microsoft.com\/learning\/en\/us\/mcse-server-infrastructure-certification.aspx\" target=\"_blank\">MCSE for <em>Server Infrastructure<\/em><\/a>, you will want to know this material. Of course, if you are running or thinking about running Hyper-V on Windows Server&nbsp;2012, you will want to know this information as well. The <a href=\"http:\/\/blogs.technet.com\/b\/virtualization\/archive\/2012\/08\/16\/introduction-to-resource-metering.aspx\" target=\"_blank\">Introduction to Resource Metering<\/a><em> <\/em>blog post written by Lalithra Fernando on the Virtualization Blog is an excellent place to start. Lalithra also wrote a blog post called <a href=\"http:\/\/blogs.technet.com\/b\/virtualization\/archive\/2012\/08\/20\/how-to-use-resource-metering-with-powershell.aspx?Redirected=true\" target=\"_blank\">How to Use Resource Metering with PowerShell<\/a><em>, <\/em>which is also a good introduction.<\/p>\n<h2>Use PowerShell to configure Resource Metering<\/h2>\n<p>One of the things is a bit confusing about Hyper-V Resource Metering on Windows Server&nbsp;2012 is that I must enable Resource Metering on the individual virtual machine, but if I want to set the Resource Metering interval, I need to do that on the server that hosts the virtual machines.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;This is the second article in a series about Hyper-V and Windows PowerShell. It is also the fourth article <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/mcse\/\" target=\"_blank\">in a series I have written about the MCSE exam 70-410<\/a>.<\/p>\n<p>Therefore, to configure the Resource Metering interval, I need to use the <strong>Set-VMHost<\/strong> cmdlet. I can do this remotely, if my logged on account has rights to the remote Hyper-V server. This is because the cmdlet has a <strong>&ndash;computername<\/strong> parameter, but no <strong>&ndash;credential<\/strong> parameter. Of course, I can wrap the thing by using Windows PowerShell remoting, and run the command on multiple remote Hyper-V servers.<\/p>\n<p>The thing that is really confusing about configuring Resource Metering is that the <strong>ResourceMeteringSaveInterval<\/strong> parameter requires a <strong>TimeSpan<\/strong> object as the input parameter. Also, the parameter only accepts whole hour values&mdash;couple this with an error in the Help example, and it is really a challenge to get this thing to work properly. (Oh, did I say that it will accept lots of things, not generate an error, and not make the change?)<\/p>\n<p>So, the first thing I do is follow the example in the Help, but an error occurs. Here is the command I typed:<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval 02: 00:00<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8461.HSG-1-28-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8461.HSG-1-28-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Well, maybe I do not need to do all that. Besides, it only accepts values from 1 &ndash; 24 hours, in whole numbers, so why should I type all those colons and stuff. So I try the following command:<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval 3<\/p>\n<p>Cool, no errors come back. But when I check the <strong>VMHost<\/strong>, I see the <strong>ResourceMeteringSaveInterval<\/strong> has not changed. These commands are shown here.<\/p>\n<p style=\"padding-left: 30px\">16:52 C:\\&gt; Set-VMHost -ResourceMeteringSaveInterval 3<\/p>\n<p style=\"padding-left: 30px\">16:56 C:\\&gt; Get-VMHost | select resource*<\/p>\n<p style=\"padding-left: 30px\">ResourceMeteringSaveInterval<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">01:00:00<\/p>\n<p>So, I try it with a leading zero. Again no error returns, but no change either.<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval 03<\/p>\n<h2>First create a legitimate TimeSpan<\/h2>\n<p>Well, dude, this is NOT really that hard. Because I KNOW how to create a new TimeSpan, in fact, the cmdlet is <strong>New-TimeSpan<\/strong>, as shown here.<\/p>\n<p style=\"padding-left: 30px\">17:00 C:\\&gt; New-TimeSpan -Hours 3<\/p>\n<p style=\"padding-left: 30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 3<\/p>\n<p style=\"padding-left: 30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 108000000000<\/p>\n<p style=\"padding-left: 30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.125<\/p>\n<p style=\"padding-left: 30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 3<\/p>\n<p style=\"padding-left: 30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 180<\/p>\n<p style=\"padding-left: 30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10800<\/p>\n<p style=\"padding-left: 30px\">TotalMilliseconds : 10800000<\/p>\n<p>So, all I have to do is store the TimeSpan in a variable and use that variable to configure the Resource Metering save interval, as shown here.<\/p>\n<p style=\"padding-left: 30px\">$rm = New-TimeSpan -Hours 3<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval $rm<\/p>\n<p style=\"padding-left: 30px\">Get-VMHost | select resource*<\/p>\n<p>The image shown here illustrates using these commands, as well as the output associated with each cmdlet.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6175.HSG-1-28-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6175.HSG-1-28-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Simplify the process<\/h2>\n<p>Well, so I found out I could actually change the value&mdash;that is good news at least. So now, of course, I want to see if I can simplify the process. The first thing I decide to do is to put the <strong>New-TimeSpan<\/strong> cmdlet in the <strong>ResourceMeteringSaveInterval<\/strong> parameter slot. Once I make the change, I use the up arrow and retrieve the <strong>Get-VMHost<\/strong> command where I choose the <strong>Resource*<\/strong> property. The output shows that the command was successful.<\/p>\n<p style=\"padding-left: 30px\">17:04 C:\\&gt; Set-VMHost -ResourceMeteringSaveInterval (New-TimeSpan -Hours 2)<\/p>\n<p style=\"padding-left: 30px\">17:09 C:\\&gt; Get-VMHost | select resource*<\/p>\n<p style=\"padding-left: 30px\">ResourceMeteringSaveInterval<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">02:00:00<\/p>\n<p>Well, that is cool, what about if I cast it to a <strong>TimeSpan<\/strong> object? I love casting stuff in Windows PowerShell. I think it is both elegant and useful. So, I use the [timespan] type accelerator to cast 01 to a <strong>TimeSpan<\/strong> object. The results appear here.<\/p>\n<p style=\"padding-left: 30px\">17:09 C:\\&gt; [timespan]01<\/p>\n<p style=\"padding-left: 30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1<\/p>\n<p style=\"padding-left: 30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1.15740740740741E-12<\/p>\n<p style=\"padding-left: 30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2.77777777777778E-11<\/p>\n<p style=\"padding-left: 30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1.66666666666667E-09<\/p>\n<p style=\"padding-left: 30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1E-07<\/p>\n<p style=\"padding-left: 30px\">TotalMilliseconds : 0.0001<\/p>\n<p>Well, it worked, but the problem is that I created a TimeSpan that is equal to 1 tick, and not to 1 hour. Bummer. So I add some zeros and look to see what happens. The output appears in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3386.HSG-1-28-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3386.HSG-1-28-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I now know that I can use [timespan]&#8221;01:00:00&#8243; and create a legitimate TimeSpan that measures 1 hour. But can I make it any easier? I drop a couple of zeros, and as shown here, it works.<\/p>\n<p style=\"padding-left: 30px\">17:14 C:\\&gt; [timespan]&#8221;01:00&#8243;<\/p>\n<p style=\"padding-left: 30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1<\/p>\n<p style=\"padding-left: 30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 36000000000<\/p>\n<p style=\"padding-left: 30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.0416666666666667<\/p>\n<p style=\"padding-left: 30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1<\/p>\n<p style=\"padding-left: 30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 60<\/p>\n<p style=\"padding-left: 30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 3600<\/p>\n<p style=\"padding-left: 30px\">TotalMilliseconds : 3600000<\/p>\n<p>Groovy, that worked as well. I am confident that it will work for the <strong>Set-VMHost<\/strong> cmdlet as well, but the only way to ensure that is to use it directly. So here goes:<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval [timespan]&#8221;12:00&#8243;<\/p>\n<p>Bummer, an error occurs. I modify the command and group the [timespan]&rdquo;12:00&rdquo;. The revised command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval ([timespan]&#8221;12:00&#8243;)<\/p>\n<p>This time the command does not generate a command, so I use the <strong>Get-VMHost<\/strong> cmdlet to see if it worked. As shown in the following image, the command works fine.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1121.HSG-1-28-13-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1121.HSG-1-28-13-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Well, I know that Windows PowerShell does automatic type conversion, and I also know from the previous errors that the <strong>&ndash;ResourceMeteringSaveInterval<\/strong> expects a <strong>TimeSpan<\/strong> object. If I can convert 01:00 to a TimeSpan, will the parameter accept it? Let&rsquo;s see &hellip;<\/p>\n<p style=\"padding-left: 30px\">17:18 C:\\&gt; Set-VMHost -ResourceMeteringSaveInterval 01:00<\/p>\n<p style=\"padding-left: 30px\">17:22 C:\\&gt; Get-VMHost | select resource*<\/p>\n<p style=\"padding-left: 30px\">ResourceMeteringSaveInterval<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">01:00:00<\/p>\n<p>Yep, it works.<\/p>\n<h2>Summary<\/h2>\n<p>I typed a lot of commands in this article. What worked? Well, storing a <strong>TimeSpan<\/strong> object in a variable works, as shown here:<\/p>\n<p style=\"padding-left: 30px\">$rm = New-TimeSpan -Hours 3<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval $rm<\/p>\n<p>Also, creating the <strong>TimeSpan<\/strong> object at the <strong>ResourceMeteringSaveInterval<\/strong> also worked, as shown here:<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval (New-Timespan -Hours 2)<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval ([timespan]&#8221;12:00&#8243;)<\/p>\n<p>Because Windows PowerShell does automatic type conversion, I can also supply a value that casts to a <strong>TimeSpan<\/strong> object, as shown here.<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval 01:00<\/p>\n<p>I can also drop the leading zero if I wish, because the following command also works.<\/p>\n<p style=\"padding-left: 30px\">Set-VMHost -ResourceMeteringSaveInterval 5:00<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell&nbsp;3.0 to configure Hyper-V Resource Metering on Windows Server&nbsp;2012. Microsoft Scripting Guy, Ed Wilson, is here. Well, today is Monday, and the first thing I do on Monday is update the Help on Windows PowerShell&nbsp;3.0. I use the command that is shown here. Update-Help -Module [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[271,402,362,3,45,368],"class_list":["post-4245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hyper-v","tag-mcse","tag-powershell-3","tag-scripting-guy","tag-windows-powershell","tag-windows-server-2012"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell&nbsp;3.0 to configure Hyper-V Resource Metering on Windows Server&nbsp;2012. Microsoft Scripting Guy, Ed Wilson, is here. Well, today is Monday, and the first thing I do on Monday is update the Help on Windows PowerShell&nbsp;3.0. I use the command that is shown here. Update-Help -Module [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4245"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4245\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}