<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Crescent City Networking &#187; Developing on Windows Virtual PC / Virtual Server / Hyper-V</title>
	<atom:link href="http://ccnetworking.com/wordpress/archives/tag/developing-on-windows-virtual-pc-virtual-server-hyper-v/feed" rel="self" type="application/rss+xml" />
	<link>http://ccnetworking.com/wordpress</link>
	<description></description>
	<lastBuildDate>Wed, 08 Feb 2012 04:39:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using CPU Reserve to enforce virtual processor ratio</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/11/03/using-cpu-reserve-to-enforce-virtual-processor-ratio.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/11/03/using-cpu-reserve-to-enforce-virtual-processor-ratio.aspx#comments</comments>
		<pubDate>Thu, 03 Nov 2011 18:45:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float: none;margin: 0px;padding: 0px 0px 0px 0px">
// 

<br /><br /></div>
<p>On Monday I posted a script for configuring virtual machine CPU scheduler settings.&#160; This script got me to thinking about another use for the virtual machine CPU reserve.</p>
<p>You see, it can also be used to ensure that you do not unintentionally start too many virtual machines at once.</p>
<p>If you were to set the CPU reserve on each virtual machine at 20% (or at 20,000 using the underlying API) then it is not possible to start extra virtual machines once you hit a ratio of 5 virtual processors for each physical processor.&#160; This is actually what System Center Virtual Machine Manager does to enforce limits on the system.</p>
<p>Here is a script that will do just that:</p>
<div style="cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;overflow: auto;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<div style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $failureString <span style="color: #006080">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      {<span style="color: #008000">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      <span style="color: #008000">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">         {write-host $job.PercentComplete <span style="color: #006080">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #008000">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       <span style="color: #008000">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          {write-host $successString}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorDescription"</span> $job.ErrorDescription}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the new CPU reservation</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$NewReservation = Read-Host <span style="color: #006080">"Specify the CPU reservation (from 0-100000)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get all VSSDs for non-snapshots</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VSSDs = gwmi <span style="color: #006080">"MSVM_VirtualSystemSettingData"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer &#124; ? {$_.SettingType <span style="color: #cc6633">-eq</span> 3}  </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #0000ff">foreach</span> ($VSSD <span style="color: #0000ff">in</span> $VSSDs)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   {</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000"># Get the related VM</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   $VM = $VSSD.getRelated(<span style="color: #006080">"MSVM_ComputerSystem"</span>) &#124; select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   <span style="color: #008000"># Get the processor setting data</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   $ProcSetting = $VSSD.getRelated(<span style="color: #006080">"Msvm_ProcessorSettingData"</span>) &#124; select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000"># Update ProcSetting with the new value</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   $ProcSetting.Reservation = $NewReservation</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   <span style="color: #008000"># Apply the changes to the processor setting data back to the virtual machine</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   $result = $VMMS.ModifyVirtualSystemResources($VM, $ProcSetting.GetText(1))</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">  </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000"># Process the result</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   $successMessage = <span style="color: #006080">"Updated processor scheduling settings on '"</span> + $VM.ElementName + <span style="color: #006080">"'"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   $failureMessage = <span style="color: #006080">"Failed to update processor scheduling settings on "</span> + $VM.ElementName + <span style="color: #006080">"'"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   ProcessResult $result $successMessage $failureMessage</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   }</pre>
<!--CRLF--></div>
</div>
<p>If you run this script and specify &#8220;20000&#8221; then you will be able to run at a ratio of 5 virtual processors for each physical processor.&#160; If you run this script and specify &#8220;25000&#8221; then you will be able to run at a ratio of 4 virtual processors for each physical processor.</p>
<p>Note that this will not apply to any virtual machine snapshots, or to any newly created virtual machines, so it is a little fragile as a solution.</p>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10232353" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float: none; margin: 0px; padding: 0px 0px 0px 0px;">
<script type="text/javascript">// < ![CDATA[
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/11/03/using-cpu-reserve-to-enforce-virtual-processor-ratio.aspx'; 
// ]]&gt;</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
<br /><br /></div>
<p>On Monday I posted a script for configuring virtual machine CPU scheduler settings.&nbsp; This script got me to thinking about another use for the virtual machine CPU reserve.</p>
<p>You see, it can also be used to ensure that you do not unintentionally start too many virtual machines at once.</p>
<p>If you were to set the CPU reserve on each virtual machine at 20% (or at 20,000 using the underlying API) then it is not possible to start extra virtual machines once you hit a ratio of 5 virtual processors for each physical processor.&nbsp; This is actually what System Center Virtual Machine Manager does to enforce limits on the system.</p>
<p>Here is a script that will do just that:</p>
<div id="codeSnippetWrapper" style="cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; overflow: auto; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 2000px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   <span style="color: #0000ff;">if</span> ($result.ReturnValue <span style="color: #cc6633;">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   ElseIf ($result.ReturnValue <span style="color: #cc6633;">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $failureString <span style="color: #006080;">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      {<span style="color: #008000;">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      <span style="color: #008000;">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      <span style="color: #0000ff;">while</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 3 -or $job.JobState <span style="color: #cc6633;">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">         {write-host $job.PercentComplete <span style="color: #006080;">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #008000;">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       <span style="color: #008000;">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       <span style="color: #0000ff;">if</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          {write-host $successString}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorDescription"</span> $job.ErrorDescription}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$HyperVServer = Read-Host <span style="color: #006080;">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the new CPU reservation</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$NewReservation = Read-Host <span style="color: #006080;">"Specify the CPU reservation (from 0-100000)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get all VSSDs for non-snapshots</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VSSDs = gwmi <span style="color: #006080;">"MSVM_VirtualSystemSettingData"</span> -namespace <span style="color: #006080;">"root\virtualization"</span> -computername $HyperVServer | ? {$_.SettingType <span style="color: #cc6633;">-eq</span> 3}  </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #0000ff;">foreach</span> ($VSSD <span style="color: #0000ff;">in</span> $VSSDs)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   {</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;"># Get the related VM</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   $VM = $VSSD.getRelated(<span style="color: #006080;">"MSVM_ComputerSystem"</span>) | select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   <span style="color: #008000;"># Get the processor setting data</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   $ProcSetting = $VSSD.getRelated(<span style="color: #006080;">"Msvm_ProcessorSettingData"</span>) | select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;"># Update ProcSetting with the new value</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   $ProcSetting.Reservation = $NewReservation</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   <span style="color: #008000;"># Apply the changes to the processor setting data back to the virtual machine</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   $result = $VMMS.ModifyVirtualSystemResources($VM, $ProcSetting.GetText(1))</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">  </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;"># Process the result</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   $successMessage = <span style="color: #006080;">"Updated processor scheduling settings on '"</span> + $VM.ElementName + <span style="color: #006080;">"'"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   $failureMessage = <span style="color: #006080;">"Failed to update processor scheduling settings on "</span> + $VM.ElementName + <span style="color: #006080;">"'"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   ProcessResult $result $successMessage $failureMessage</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   }</pre>
<!--CRLF--></div>
</div>
<p>If you run this script and specify &ldquo;20000&rdquo; then you will be able to run at a ratio of 5 virtual processors for each physical processor.&nbsp; If you run this script and specify &ldquo;25000&rdquo; then you will be able to run at a ratio of 4 virtual processors for each physical processor.</p>
<p>Note that this will not apply to any virtual machine snapshots, or to any newly created virtual machines, so it is a little fragile as a solution.</p>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10232353" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/11/03/using-cpu-reserve-to-enforce-virtual-processor-ratio.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-23-23-53/SetProcReservationOnAll.zip" length="1358" type="application/zip" />
		</item>
		<item>
		<title>Setting VM CPU scheduler settings with PowerShell [Hyper-V]</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/31/setting-vm-cpu-scheduler-settings-with-powershell-hyper-v.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/31/setting-vm-cpu-scheduler-settings-with-powershell-hyper-v.aspx#comments</comments>
		<pubDate>Tue, 01 Nov 2011 02:51:19 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float:none;margin:0px;padding:0px 0px 0px 0px"> tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/31/setting-vm-cpu-scheduler-settings-with-powershell-hyper-v.aspx';  <br /><br /></div><p>A while ago I did a number of posts talking about the different controls that are available to you in regards to how Hyper-V schedules virtual machine processor resources (<a title="Hyper-V CPU Scheduling–Part 1" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/02/14/hyper-v-cpu-scheduling-part-1.aspx">Hyper-V CPU Scheduling–Part 1</a>, <a title="Hyper-V CPU Scheduling–Part 2" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/02/15/hyper-v-cpu-scheduling-part-2.aspx">Hyper-V CPU Scheduling–Part 2</a>, <a title="Hyper-V CPU Scheduling–Part 3" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/02/17/hyper-v-cpu-scheduling-part-3.aspx">Hyper-V CPU Scheduling–Part 3</a> and <a title="Hyper-V CPU Scheduling–Part 4" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/02/18/hyper-v-cpu-scheduling-part-4.aspx">Hyper-V CPU Scheduling–Part 4</a>).&#160; Today I want to share a PowerShell script that shows you how to configure these settings programmatically: </p>  <div>   <div>     <pre><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->

    <pre>Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->

    <pre>{</pre>
<!--CRLF-->

    <pre>   <span style="color: #008000">#Return success if the return value is &#34;0&#34;</span></pre>
<!--CRLF-->

    <pre>   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--CRLF-->

    <pre>      {write-host $successString} </pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre>   <span style="color: #008000">#If the return value is not &#34;0&#34; or &#34;4096&#34; then the operation failed</span></pre>
<!--CRLF-->

    <pre>   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--CRLF-->

    <pre>      {write-host $failureString <span style="color: #006080">&#34;  Error value:&#34;</span> $result.ReturnValue}</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre>   Else</pre>
<!--CRLF-->

    <pre>      {<span style="color: #008000">#Get the job object</span></pre>
<!--CRLF-->

    <pre>      $job=[WMI]$result.job</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre>      <span style="color: #008000">#Provide updates if the jobstate is &#34;3&#34; (starting) or &#34;4&#34; (running)</span></pre>
<!--CRLF-->

    <pre>      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--CRLF-->

    <pre>         {write-host $job.PercentComplete <span style="color: #006080">&#34;% complete&#34;</span></pre>
<!--CRLF-->

    <pre>          start-sleep 1</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre>          <span style="color: #008000">#Refresh the job object</span></pre>
<!--CRLF-->

    <pre>          $job=[WMI]$result.job}</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre>       <span style="color: #008000">#A jobstate of &#34;7&#34; means success</span></pre>
<!--CRLF-->

    <pre>       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--CRLF-->

    <pre>          {write-host $successString}</pre>
<!--CRLF-->

    <pre>       Else</pre>
<!--CRLF-->

    <pre>          {write-host $failureString</pre>
<!--CRLF-->

    <pre>          write-host <span style="color: #006080">&#34;ErrorCode:&#34;</span> $job.ErrorCode</pre>
<!--CRLF-->

    <pre>          write-host <span style="color: #006080">&#34;ErrorDescription&#34;</span> $job.ErrorDescription}</pre>
<!--CRLF-->

    <pre>       }</pre>
<!--CRLF-->

    <pre>}</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->

    <pre>$HyperVServer = Read-Host <span style="color: #006080">&#34;Specify the Hyper-V Server to use (enter '.' for the local computer)&#34;</span></pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->

    <pre>$VMName = Read-Host <span style="color: #006080">&#34;Specify the name of the virtual machine&#34;</span></pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->

    <pre>$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--CRLF-->

    <pre>$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">&#34;ElementName='$VMName'&#34;</span> -namespace <span style="color: #006080">&#34;root\virtualization&#34;</span> -computername $HyperVServer</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre><span style="color: #008000"># SettingType = 3 ensures that we do not get snapshots</span></pre>
<!--CRLF-->

    <pre>$SystemSettingData = $VM.getRelated(<span style="color: #006080">&#34;Msvm_VirtualSystemSettingData&#34;</span>) &#124; where {$_.SettingType <span style="color: #cc6633">-eq</span> 3}</pre>
<!--CRLF-->

    <pre>&#160;</pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Get the processor setting data</span></pre>
<!--CRLF-->

    <pre>$ProcSetting = $SystemSettingData.getRelated(<span style="color: #006080">&#34;Msvm_ProcessorSettingData&#34;</span>) &#124; select -first 1</pre>
<!--CRLF-->

    <pre>&#160;</pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Get new values from the user</span></pre>
<!--CRLF-->

    <pre>$NewReservation = Read-Host <span style="color: #006080">&#34;Specify the CPU reserve (from 0-10000) [Currently:&#34;</span>$ProcSetting.Reservation<span style="color: #006080">&#34;]&#34;</span></pre>
<!--CRLF-->

    <pre>$NewLimit = Read-Host <span style="color: #006080">&#34;Specify the CPU limit (from 0-10000) [Currently:&#34;</span>$ProcSetting.Limit<span style="color: #006080">&#34;]&#34;</span></pre>
<!--CRLF-->

    <pre>$NewWeight = Read-Host <span style="color: #006080">&#34;Specify the CPU weight (from 0-10000) [Currently:&#34;</span>$ProcSetting.weight<span style="color: #006080">&#34;]&#34;</span></pre>
<!--CRLF-->

    <pre>&#160;</pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Update ProcSetting with the new values</span></pre>
<!--CRLF-->

    <pre>$ProcSetting.Reservation = $NewReservation</pre>
<!--CRLF-->

    <pre>$ProcSetting.Limit = $NewLimit</pre>
<!--CRLF-->

    <pre>$ProcSetting.weight = $NewWeight</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Apply the changes to the processor setting data back to the virtual machine</span></pre>
<!--CRLF-->

    <pre>$result = $VMMS.ModifyVirtualSystemResources($VM, $ProcSetting.GetText(1))</pre>
<!--CRLF-->

    <pre> </pre>
<!--CRLF-->

    <pre><span style="color: #008000"># Process the result</span></pre>
<!--CRLF-->

    <pre>ProcessResult $result <span style="color: #006080">&#34;Updated processor scheduling settings.&#34;</span> <span style="color: #006080">&#34;Failed to update processor scheduling settings.&#34;</span></pre>
<!--CRLF--></div>
</div>

<p>Cheers, 
  <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10231919" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float:none; margin:0px; padding:0px 0px 0px 0px;"><script type="text/javascript"> tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/31/setting-vm-cpu-scheduler-settings-with-powershell-hyper-v.aspx'; </script> <script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script><br /><br /></div><p>A while ago I did a number of posts talking about the different controls that are available to you in regards to how Hyper-V schedules virtual machine processor resources (<a title="Hyper-V CPU Scheduling–Part 1" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/02/14/hyper-v-cpu-scheduling-part-1.aspx">Hyper-V CPU Scheduling–Part 1</a>, <a title="Hyper-V CPU Scheduling–Part 2" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/02/15/hyper-v-cpu-scheduling-part-2.aspx">Hyper-V CPU Scheduling–Part 2</a>, <a title="Hyper-V CPU Scheduling–Part 3" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/02/17/hyper-v-cpu-scheduling-part-3.aspx">Hyper-V CPU Scheduling–Part 3</a> and <a title="Hyper-V CPU Scheduling–Part 4" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/02/18/hyper-v-cpu-scheduling-part-4.aspx">Hyper-V CPU Scheduling–Part 4</a>).&#160; Today I want to share a PowerShell script that shows you how to configure these settings programmatically: </p>  <div id="codeSnippetWrapper" style="cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: &#39;Courier New&#39;, courier, monospace; border-right: silver 1px solid; width: 97.5%; border-bottom: silver 1px solid; overflow: auto; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 2000px; background-color: #f4f4f4">   <div id="codeSnippet" style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">     <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">{</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">   <span style="color: #008000">#Return success if the return value is &quot;0&quot;</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">      {write-host $successString} </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">   <span style="color: #008000">#If the return value is not &quot;0&quot; or &quot;4096&quot; then the operation failed</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">      {write-host $failureString <span style="color: #006080">&quot;  Error value:&quot;</span> $result.ReturnValue}</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">   Else</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">      {<span style="color: #008000">#Get the job object</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">      $job=[WMI]$result.job</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">      <span style="color: #008000">#Provide updates if the jobstate is &quot;3&quot; (starting) or &quot;4&quot; (running)</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">         {write-host $job.PercentComplete <span style="color: #006080">&quot;% complete&quot;</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">          start-sleep 1</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">          <span style="color: #008000">#Refresh the job object</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">          $job=[WMI]$result.job}</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">       <span style="color: #008000">#A jobstate of &quot;7&quot; means success</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">          {write-host $successString}</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">       Else</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">          {write-host $failureString</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">          write-host <span style="color: #006080">&quot;ErrorCode:&quot;</span> $job.ErrorCode</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">          write-host <span style="color: #006080">&quot;ErrorDescription&quot;</span> $job.ErrorDescription}</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">       }</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">}</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">$HyperVServer = Read-Host <span style="color: #006080">&quot;Specify the Hyper-V Server to use (enter '.' for the local computer)&quot;</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">$VMName = Read-Host <span style="color: #006080">&quot;Specify the name of the virtual machine&quot;</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">&quot;ElementName='$VMName'&quot;</span> -namespace <span style="color: #006080">&quot;root\virtualization&quot;</span> -computername $HyperVServer</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #008000"># SettingType = 3 ensures that we do not get snapshots</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">$SystemSettingData = $VM.getRelated(<span style="color: #006080">&quot;Msvm_VirtualSystemSettingData&quot;</span>) | where {$_.SettingType <span style="color: #cc6633">-eq</span> 3}</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">&#160;</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"><span style="color: #008000"># Get the processor setting data</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">$ProcSetting = $SystemSettingData.getRelated(<span style="color: #006080">&quot;Msvm_ProcessorSettingData&quot;</span>) | select -first 1</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">&#160;</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #008000"># Get new values from the user</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">$NewReservation = Read-Host <span style="color: #006080">&quot;Specify the CPU reserve (from 0-10000) [Currently:&quot;</span>$ProcSetting.Reservation<span style="color: #006080">&quot;]&quot;</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">$NewLimit = Read-Host <span style="color: #006080">&quot;Specify the CPU limit (from 0-10000) [Currently:&quot;</span>$ProcSetting.Limit<span style="color: #006080">&quot;]&quot;</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">$NewWeight = Read-Host <span style="color: #006080">&quot;Specify the CPU weight (from 0-10000) [Currently:&quot;</span>$ProcSetting.weight<span style="color: #006080">&quot;]&quot;</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">&#160;</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"><span style="color: #008000"># Update ProcSetting with the new values</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">$ProcSetting.Reservation = $NewReservation</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">$ProcSetting.Limit = $NewLimit</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">$ProcSetting.weight = $NewWeight</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #008000"># Apply the changes to the processor setting data back to the virtual machine</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white">$result = $VMMS.ModifyVirtualSystemResources($VM, $ProcSetting.GetText(1))</pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"> </pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: white"><span style="color: #008000"># Process the result</span></pre>
<!--CRLF-->

    <pre style="border-top-style: none; font-size: 8pt; font-family: &#39;Courier New&#39;, courier, monospace; width: 100%; border-bottom-style: none; color: black; overflow: visible; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">ProcessResult $result <span style="color: #006080">&quot;Updated processor scheduling settings.&quot;</span> <span style="color: #006080">&quot;Failed to update processor scheduling settings.&quot;</span></pre>
<!--CRLF--></div>
</div>

<p>Cheers, 
  <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10231919" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/31/setting-vm-cpu-scheduler-settings-with-powershell-hyper-v.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How many virtual processors does a virtual machine have?</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/25/how-many-virtual-processors-does-a-virtual-machine-have.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/25/how-many-virtual-processors-does-a-virtual-machine-have.aspx#comments</comments>
		<pubDate>Tue, 25 Oct 2011 15:17:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float: none;margin: 0px;padding: 0px 0px 0px 0px">
// 

<br /><br /></div>
<p>A recent forum poster asked:</p>
<p>How do I write a script that figures out how many virtual processors a virtual machine has, when it is offline (i.e. not running)?</p>
<p>His question showed that he had made a classic mistake when working with the Hyper-V WMI APIs.&#160; You see Hyper-V usually has two WMI objects for many parts of the virtual machine &#8211; one of them representing the settings and one of them representing the live object itself.</p>
<p>Case in hand: for a virtual processor you have &#8220;MSVM_Processor&#8221; which represents the live object, and you have &#8220;MSVM_ProcessorSettingData&#8221; which represents its settings.</p>
<p>Looking at the wrong object at the wrong time can cause problems.&#160; Live objects should be used for gathering usage information about running virtual machines &#8211; settings object should be used for getting and setting configuration details.&#160; 90% of the time &#8211; the settings object is what you want (and 90% of the time it is the object that ends with the string &#8220;SettingData&#8221;).</p>
<p>Here is a simple script that gets a virtual machines processor count using both objects:</p>
<div style="cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;overflow: auto;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<div style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VMName = Read-Host <span style="color: #006080">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">"ElementName='$VMName'"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Set the stage</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">Write-host <span style="color: #006080">"There are two ways to get a virtual machines processor count, the wrong way and the right way..."</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Let's do the wrong way first</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get the virtual processor objects</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$vProcs = $vm.GetRelated(<span style="color: #006080">"MSVM_Processor"</span>)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">Write-host <span style="color: #006080">"Doing it the wrong way (using Msvm_Processor):"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">write-host <span style="color: #006080">"The"</span> $VMName <span style="color: #006080">"virtual machine is configured with"</span> $vProcs.count <span style="color: #006080">"virtual processors."</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Now let's do it the right way</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># SettingType = 3 ensures that we do not get snapshots</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$SystemSettingData = $VM.getRelated(<span style="color: #006080">"Msvm_VirtualSystemSettingData"</span>) &#124; where {$_.SettingType <span style="color: #cc6633">-eq</span> 3}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Get the processor setting data</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$ProcSetting = $SystemSettingData.getRelated(<span style="color: #006080">"Msvm_ProcessorSettingData"</span>) &#124; select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">Write-host <span style="color: #006080">"Doing it the right way (using Msvm_ProcessorSettingData):"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">write-host <span style="color: #006080">"The"</span> $VMName <span style="color: #006080">"virtual machine is configured with"</span> $ProcSetting.VirtualQuantity <span style="color: #006080">"virtual processors."</span></pre>
<!--CRLF--></div>
</div>
<p>If I run this on a running virtual machine &#8211; both methods return the same thing:</p>
<p><a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-41-22-metablogapi/8176.image_5F00_1F40D5A7.png"><img style="padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-41-22-metablogapi/7080.image_5F00_thumb_5F00_05D8D26D.png" width="445" height="289" /></a></p>
<p>But if the virtual machine is not running &#8211; only the MVSM_ProcessorSettingData actually tells me what I want to know:</p>
<p><a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-41-22-metablogapi/2625.image_5F00_05006C83.png"><img style="padding-top: 0px;padding-left: 0px;padding-right: 0px;border-width: 0px" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-41-22-metablogapi/5023.image_5F00_thumb_5F00_0BB37606.png" width="448" height="291" /></a></p>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10229148" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float: none; margin: 0px; padding: 0px 0px 0px 0px;">
<script type="text/javascript">// < ![CDATA[
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/25/how-many-virtual-processors-does-a-virtual-machine-have.aspx';
// ]]&gt;</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
<br /><br /></div>
<p>A recent forum poster asked:</p>
<p>How do I write a script that figures out how many virtual processors a virtual machine has, when it is offline (i.e. not running)?</p>
<p>His question showed that he had made a classic mistake when working with the Hyper-V WMI APIs.&nbsp; You see Hyper-V usually has two WMI objects for many parts of the virtual machine &ndash; one of them representing the settings and one of them representing the live object itself.</p>
<p>Case in hand: for a virtual processor you have &ldquo;MSVM_Processor&rdquo; which represents the live object, and you have &ldquo;MSVM_ProcessorSettingData&rdquo; which represents its settings.</p>
<p>Looking at the wrong object at the wrong time can cause problems.&nbsp; Live objects should be used for gathering usage information about running virtual machines &ndash; settings object should be used for getting and setting configuration details.&nbsp; 90% of the time &ndash; the settings object is what you want (and 90% of the time it is the object that ends with the string &ldquo;SettingData&rdquo;).</p>
<p>Here is a simple script that gets a virtual machines processor count using both objects:</p>
<div id="codeSnippetWrapper" style="cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; overflow: auto; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 2000px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$HyperVServer = Read-Host <span style="color: #006080;">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VMName = Read-Host <span style="color: #006080;">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff;">filter</span> <span style="color: #006080;">"ElementName='$VMName'"</span> -namespace <span style="color: #006080;">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Set the stage</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">Write-host <span style="color: #006080;">"There are two ways to get a virtual machines processor count, the wrong way and the right way..."</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Let's do the wrong way first</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the virtual processor objects</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$vProcs = $vm.GetRelated(<span style="color: #006080;">"MSVM_Processor"</span>)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">Write-host <span style="color: #006080;">"Doing it the wrong way (using Msvm_Processor):"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">write-host <span style="color: #006080;">"The"</span> $VMName <span style="color: #006080;">"virtual machine is configured with"</span> $vProcs.count <span style="color: #006080;">"virtual processors."</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Now let's do it the right way</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># SettingType = 3 ensures that we do not get snapshots</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$SystemSettingData = $VM.getRelated(<span style="color: #006080;">"Msvm_VirtualSystemSettingData"</span>) | where {$_.SettingType <span style="color: #cc6633;">-eq</span> 3}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the processor setting data</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$ProcSetting = $SystemSettingData.getRelated(<span style="color: #006080;">"Msvm_ProcessorSettingData"</span>) | select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">Write-host <span style="color: #006080;">"Doing it the right way (using Msvm_ProcessorSettingData):"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">write-host <span style="color: #006080;">"The"</span> $VMName <span style="color: #006080;">"virtual machine is configured with"</span> $ProcSetting.VirtualQuantity <span style="color: #006080;">"virtual processors."</span></pre>
<!--CRLF--></div>
</div>
<p>If I run this on a running virtual machine &ndash; both methods return the same thing:</p>
<p><a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-41-22-metablogapi/8176.image_5F00_1F40D5A7.png"><img title="image" style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-41-22-metablogapi/7080.image_5F00_thumb_5F00_05D8D26D.png" width="445" height="289" /></a></p>
<p>But if the virtual machine is not running &ndash; only the MVSM_ProcessorSettingData actually tells me what I want to know:</p>
<p><a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-41-22-metablogapi/2625.image_5F00_05006C83.png"><img title="image" style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-41-22-metablogapi/5023.image_5F00_thumb_5F00_0BB37606.png" width="448" height="291" /></a></p>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10229148" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/25/how-many-virtual-processors-does-a-virtual-machine-have.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-22-91-48/VMProcCount.zip" length="895" type="application/zip" />
		</item>
		<item>
		<title>Deleting a snapshot sub-tree–via PowerShell [Hyper-V]</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/21/deleting-a-snapshot-subtree-via-powershell-hyper-v.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/21/deleting-a-snapshot-subtree-via-powershell-hyper-v.aspx#comments</comments>
		<pubDate>Fri, 21 Oct 2011 17:27:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float: none;margin: 0px;padding: 0px 0px 0px 0px">
// 

<br /><br /></div>
<p>Here is a dangerous script!&#160; Given a virtual machine name and a snapshot name, it will delete the specified snapshot *and* any snapshots underneath it in the snapshot tree that Hyper-V manager displays:</p>
<p></p>
<div style="cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;overflow: auto;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<div style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $failureString <span style="color: #006080">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      {<span style="color: #008000">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      <span style="color: #008000">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">         {write-host $job.PercentComplete <span style="color: #006080">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #008000">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       <span style="color: #008000">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          {write-host $successString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #0000ff">return</span> $true}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorDescription"</span> $job.ErrorDescription</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          <span style="color: #0000ff">return</span> $false}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMName = Read-Host <span style="color: #006080">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the name of the snapshot to delete the tree from</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$SnapshotName = Read-Host <span style="color: #006080">"Specify the name of the snapshot to delete the tree from (warning - a lot of snapshots will be deleted)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">"ElementName='$VMName'"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Find the snapshot that we want to delete</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$Snapshot = gwmi -Namespace root\virtualization -Query <span style="color: #006080">"Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"</span> &#124; where {$_.ElementName <span style="color: #cc6633">-eq</span> $SnapshotName} &#124; select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Delete the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$result = $VMMS.RemoveVirtualSystemSnapshotTree($Snapshot)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Check to make sure we succeeded</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$deleteSucceeded = ProcessResult $result <span style="color: #006080">"Deleted snapshot tree."</span> <span style="color: #006080">"Failed to delete snapshot tree."</span></pre>
<!--CRLF--></div>
</div>
<p>Note that this script actually uses a different WMI method to delete the snapshot sub-tree (when compared to deleting a single snapshot).&#160; There is no way to stop the deletion once it is started, and there is no confirmation prompt in this script &#8211; so be careful when you use it!</p>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225594" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float: none; margin: 0px; padding: 0px 0px 0px 0px;">
<script type="text/javascript">// < ![CDATA[
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/21/deleting-a-snapshot-subtree-via-powershell-hyper-v.aspx'; 
// ]]&gt;</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
<br /><br /></div>
<p>Here is a dangerous script!&nbsp; Given a virtual machine name and a snapshot name, it will delete the specified snapshot *and* any snapshots underneath it in the snapshot tree that Hyper-V manager displays:</p>
<p></p>
<div id="codeSnippetWrapper" style="cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; overflow: auto; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 2000px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   <span style="color: #0000ff;">if</span> ($result.ReturnValue <span style="color: #cc6633;">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   ElseIf ($result.ReturnValue <span style="color: #cc6633;">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $failureString <span style="color: #006080;">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      {<span style="color: #008000;">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      <span style="color: #008000;">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      <span style="color: #0000ff;">while</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 3 -or $job.JobState <span style="color: #cc6633;">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">         {write-host $job.PercentComplete <span style="color: #006080;">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #008000;">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       <span style="color: #008000;">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       <span style="color: #0000ff;">if</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          {write-host $successString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #0000ff;">return</span> $true}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorDescription"</span> $job.ErrorDescription</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          <span style="color: #0000ff;">return</span> $false}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$HyperVServer = Read-Host <span style="color: #006080;">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMName = Read-Host <span style="color: #006080;">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the name of the snapshot to delete the tree from</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$SnapshotName = Read-Host <span style="color: #006080;">"Specify the name of the snapshot to delete the tree from (warning - a lot of snapshots will be deleted)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff;">filter</span> <span style="color: #006080;">"ElementName='$VMName'"</span> -namespace <span style="color: #006080;">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Find the snapshot that we want to delete</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$Snapshot = gwmi -Namespace root\virtualization -Query <span style="color: #006080;">"Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"</span> | where {$_.ElementName <span style="color: #cc6633;">-eq</span> $SnapshotName} | select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Delete the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$result = $VMMS.RemoveVirtualSystemSnapshotTree($Snapshot)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Check to make sure we succeeded</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$deleteSucceeded = ProcessResult $result <span style="color: #006080;">"Deleted snapshot tree."</span> <span style="color: #006080;">"Failed to delete snapshot tree."</span></pre>
<!--CRLF--></div>
</div>
<p>Note that this script actually uses a different WMI method to delete the snapshot sub-tree (when compared to deleting a single snapshot).&nbsp; There is no way to stop the deletion once it is started, and there is no confirmation prompt in this script &ndash; so be careful when you use it!</p>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225594" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/21/deleting-a-snapshot-subtree-via-powershell-hyper-v.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-22-55-94/DeleteSnapshotTree.zip" length="1305" type="application/zip" />
		</item>
		<item>
		<title>Deleting a snapshot–via PowerShell [Hyper-V]</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/20/deleting-a-snapshot-via-powershell-hyper-v.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/20/deleting-a-snapshot-via-powershell-hyper-v.aspx#comments</comments>
		<pubDate>Thu, 20 Oct 2011 17:56:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float: none;margin: 0px;padding: 0px 0px 0px 0px">
// 

<br /><br /></div>
<p>Here is a script that will allow you to delete a single snapshot from a Hyper-V virtual machine:</p>
<div style="cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;overflow: auto;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<div style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $failureString <span style="color: #006080">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      {<span style="color: #008000">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      <span style="color: #008000">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">         {write-host $job.PercentComplete <span style="color: #006080">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #008000">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       <span style="color: #008000">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          {write-host $successString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #0000ff">return</span> $true}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorDescription"</span> $job.ErrorDescription</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          <span style="color: #0000ff">return</span> $false}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMName = Read-Host <span style="color: #006080">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the name of the snapshot to delete</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$SnapshotName = Read-Host <span style="color: #006080">"Specify the name of the snapshot to delete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">"ElementName='$VMName'"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Find the snapshot that we want to delete</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$Snapshot = gwmi -Namespace root\virtualization -Query <span style="color: #006080">"Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"</span> &#124; where {$_.ElementName <span style="color: #cc6633">-eq</span> $SnapshotName} &#124; select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Delete the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$result = $VMMS.RemoveVirtualSystemSnapshot($Snapshot)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Check to make sure we succeeded</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$deleteSucceeded = ProcessResult $result <span style="color: #006080">"Deleted snapshot."</span> <span style="color: #006080">"Failed to delete snapshot."</span></pre>
<!--CRLF--></div>
</div>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225592" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float: none; margin: 0px; padding: 0px 0px 0px 0px;">
<script type="text/javascript">// < ![CDATA[
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/20/deleting-a-snapshot-via-powershell-hyper-v.aspx'; 
// ]]&gt;</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
<br /><br /></div>
<p>Here is a script that will allow you to delete a single snapshot from a Hyper-V virtual machine:</p>
<div id="codeSnippetWrapper" style="cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; overflow: auto; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 2000px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   <span style="color: #0000ff;">if</span> ($result.ReturnValue <span style="color: #cc6633;">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   ElseIf ($result.ReturnValue <span style="color: #cc6633;">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $failureString <span style="color: #006080;">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      {<span style="color: #008000;">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      <span style="color: #008000;">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      <span style="color: #0000ff;">while</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 3 -or $job.JobState <span style="color: #cc6633;">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">         {write-host $job.PercentComplete <span style="color: #006080;">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #008000;">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       <span style="color: #008000;">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       <span style="color: #0000ff;">if</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          {write-host $successString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #0000ff;">return</span> $true}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorDescription"</span> $job.ErrorDescription</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          <span style="color: #0000ff;">return</span> $false}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$HyperVServer = Read-Host <span style="color: #006080;">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMName = Read-Host <span style="color: #006080;">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the name of the snapshot to delete</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$SnapshotName = Read-Host <span style="color: #006080;">"Specify the name of the snapshot to delete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff;">filter</span> <span style="color: #006080;">"ElementName='$VMName'"</span> -namespace <span style="color: #006080;">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Find the snapshot that we want to delete</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$Snapshot = gwmi -Namespace root\virtualization -Query <span style="color: #006080;">"Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"</span> | where {$_.ElementName <span style="color: #cc6633;">-eq</span> $SnapshotName} | select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Delete the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$result = $VMMS.RemoveVirtualSystemSnapshot($Snapshot)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Check to make sure we succeeded</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$deleteSucceeded = ProcessResult $result <span style="color: #006080;">"Deleted snapshot."</span> <span style="color: #006080;">"Failed to delete snapshot."</span></pre>
<!--CRLF--></div>
</div>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225592" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/20/deleting-a-snapshot-via-powershell-hyper-v.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-22-55-92/DeleteSnapshot.zip" length="1248" type="application/zip" />
		</item>
		<item>
		<title>Applying a snapshot–via PowerShell [Hyper-V]</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/19/applying-a-snapshot-via-powershell-hyper-v.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/19/applying-a-snapshot-via-powershell-hyper-v.aspx#comments</comments>
		<pubDate>Wed, 19 Oct 2011 17:08:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float: none;margin: 0px;padding: 0px 0px 0px 0px">
// 

<br /><br /></div>
<p>Now that we know how to take a snapshot, and how to list snapshots &#8211; let&#8217;s start applying snapshots:</p>
<div style="cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;overflow: auto;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<div style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $failureString <span style="color: #006080">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      {<span style="color: #008000">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      <span style="color: #008000">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">         {write-host $job.PercentComplete <span style="color: #006080">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #008000">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       <span style="color: #008000">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          {write-host $successString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #0000ff">return</span> $true}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorDescription"</span> $job.ErrorDescription</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          <span style="color: #0000ff">return</span> $false}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMName = Read-Host <span style="color: #006080">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the name of the snapshot to apply</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$SnapshotName = Read-Host <span style="color: #006080">"Specify the name of the snapshot to apply"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">"ElementName='$VMName'"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Find the snapshot that we want to apply</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$Snapshot = gwmi -Namespace root\virtualization -Query <span style="color: #006080">"Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"</span> &#124; where {$_.ElementName <span style="color: #cc6633">-eq</span> $SnapshotName} &#124; select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Apply the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$result = $VMMS.ApplyVirtualSystemSnapshot($VM, $Snapshot)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Check to make sure we succeeded</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$applySucceeded = ProcessResult $result <span style="color: #006080">"Snapshot applied."</span> <span style="color: #006080">"Failed to apply snapshot."</span></pre>
<!--CRLF--></div>
</div>
<p>This is a fairly standard Hyper-V WMI script.&#160; One thing to be aware of is that this operation will fail if the virtual machine is running when you try to apply the snapshot &#8211; the virtual machine needs to be turned off or put into a saved state first.</p>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225588" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float: none; margin: 0px; padding: 0px 0px 0px 0px;">
<script type="text/javascript">// < ![CDATA[
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/19/applying-a-snapshot-via-powershell-hyper-v.aspx'; 
// ]]&gt;</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
<br /><br /></div>
<p>Now that we know how to take a snapshot, and how to list snapshots &ndash; let&rsquo;s start applying snapshots:</p>
<div id="codeSnippetWrapper" style="cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; overflow: auto; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 2000px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   <span style="color: #0000ff;">if</span> ($result.ReturnValue <span style="color: #cc6633;">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   ElseIf ($result.ReturnValue <span style="color: #cc6633;">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $failureString <span style="color: #006080;">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      {<span style="color: #008000;">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      <span style="color: #008000;">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      <span style="color: #0000ff;">while</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 3 -or $job.JobState <span style="color: #cc6633;">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">         {write-host $job.PercentComplete <span style="color: #006080;">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #008000;">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       <span style="color: #008000;">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       <span style="color: #0000ff;">if</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          {write-host $successString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #0000ff;">return</span> $true}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorDescription"</span> $job.ErrorDescription</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          <span style="color: #0000ff;">return</span> $false}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$HyperVServer = Read-Host <span style="color: #006080;">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMName = Read-Host <span style="color: #006080;">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the name of the snapshot to apply</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$SnapshotName = Read-Host <span style="color: #006080;">"Specify the name of the snapshot to apply"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff;">filter</span> <span style="color: #006080;">"ElementName='$VMName'"</span> -namespace <span style="color: #006080;">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Find the snapshot that we want to apply</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$Snapshot = gwmi -Namespace root\virtualization -Query <span style="color: #006080;">"Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"</span> | where {$_.ElementName <span style="color: #cc6633;">-eq</span> $SnapshotName} | select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Apply the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$result = $VMMS.ApplyVirtualSystemSnapshot($VM, $Snapshot)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Check to make sure we succeeded</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$applySucceeded = ProcessResult $result <span style="color: #006080;">"Snapshot applied."</span> <span style="color: #006080;">"Failed to apply snapshot."</span></pre>
<!--CRLF--></div>
</div>
<p>This is a fairly standard Hyper-V WMI script.&nbsp; One thing to be aware of is that this operation will fail if the virtual machine is running when you try to apply the snapshot &ndash; the virtual machine needs to be turned off or put into a saved state first.</p>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225588" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/19/applying-a-snapshot-via-powershell-hyper-v.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-22-55-88/ApplySnapshot.zip" length="1247" type="application/zip" />
		</item>
		<item>
		<title>Listing snapshots–via PowerShell [Hyper-V]</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/18/listing-snapshots-via-powershell-hyper-v.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/18/listing-snapshots-via-powershell-hyper-v.aspx#comments</comments>
		<pubDate>Tue, 18 Oct 2011 16:57:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float: none;margin: 0px;padding: 0px 0px 0px 0px">
// 

<br /><br /></div>
<p>After the relatively trick &#8220;taking a snapshot&#8221; script &#8211; today&#8217;s snapshot script is quite easy.&#160; Listing the snapshots that a virtual machine has:</p>
<div style="cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;overflow: auto;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<div style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VMName = Read-Host <span style="color: #006080">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">"ElementName='$VMName'"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get snapshot objects associated with the virtual machine</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$Snapshots = gwmi -Namespace root\virtualization -Query <span style="color: #006080">"Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"</span> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">write-host $VMName <span style="color: #006080">"has the following snapshots:"</span> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Display a formatted list of snapshots</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$Snapshots &#124; sort CreationTime &#124; Format-Table -Property @{n=<span style="color: #006080">'Snapshot Name'</span>;e={$_.ElementName}}</pre>
<!--CRLF--></div>
</div>
<p>One thing to point out here is that I am using a WMI query that looks at the association class (MSVM_ElementSettingData) as well as the result class (MSVM_VirtualSystemSettingData).&#160; The reason why I do this is that if you grab all MSVM_VirtualSystemSettingData classes that are associated with a virtual machine &#8211; you will get two extra entries: one for the active virtual machine, and one for the latest snapshot (which results in one of your snapshots being duplicated).&#160; The method that I am using here ensures that you only get snapshots, and you do not get any duplicates.</p>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225584" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float: none; margin: 0px; padding: 0px 0px 0px 0px;">
<script type="text/javascript">// < ![CDATA[
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/18/listing-snapshots-via-powershell-hyper-v.aspx'; 
// ]]&gt;</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
<br /><br /></div>
<p>After the relatively trick &ldquo;taking a snapshot&rdquo; script &ndash; today&rsquo;s snapshot script is quite easy.&nbsp; Listing the snapshots that a virtual machine has:</p>
<div id="codeSnippetWrapper" style="cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; overflow: auto; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 2000px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$HyperVServer = Read-Host <span style="color: #006080;">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VMName = Read-Host <span style="color: #006080;">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff;">filter</span> <span style="color: #006080;">"ElementName='$VMName'"</span> -namespace <span style="color: #006080;">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get snapshot objects associated with the virtual machine</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$Snapshots = gwmi -Namespace root\virtualization -Query <span style="color: #006080;">"Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData"</span> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">write-host</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">write-host $VMName <span style="color: #006080;">"has the following snapshots:"</span> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Display a formatted list of snapshots</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$Snapshots | sort CreationTime | Format-Table -Property @{n=<span style="color: #006080;">'Snapshot Name'</span>;e={$_.ElementName}}</pre>
<!--CRLF--></div>
</div>
<p>One thing to point out here is that I am using a WMI query that looks at the association class (MSVM_ElementSettingData) as well as the result class (MSVM_VirtualSystemSettingData).&nbsp; The reason why I do this is that if you grab all MSVM_VirtualSystemSettingData classes that are associated with a virtual machine &ndash; you will get two extra entries: one for the active virtual machine, and one for the latest snapshot (which results in one of your snapshots being duplicated).&nbsp; The method that I am using here ensures that you only get snapshots, and you do not get any duplicates.</p>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225584" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/18/listing-snapshots-via-powershell-hyper-v.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-22-55-84/ListSnapshots.zip" length="744" type="application/zip" />
		</item>
		<item>
		<title>Taking a snapshot–via PowerShell [Hyper-V]</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/17/taking-a-snapshot-via-powershell-hyper-v.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/17/taking-a-snapshot-via-powershell-hyper-v.aspx#comments</comments>
		<pubDate>Mon, 17 Oct 2011 16:51:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float: none;margin: 0px;padding: 0px 0px 0px 0px">
// 

<br /><br /></div>
<p>This week I am going to be sharing a bunch of scripts that show you how to interact with virtual machine snapshots in Hyper-V.&#160; And I am going to start off with the hardest one &#8211; taking a snapshot:</p>
<div class="csharpcode">
<div style="cursor: text;font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 97.5%;overflow: auto;direction: ltr;text-align: left;margin: 20px 0px 10px;line-height: 12pt;background-color: #f4f4f4;border: silver 1px solid;padding: 4px">
<div style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   <span style="color: #008000">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      {write-host $failureString <span style="color: #006080">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      {<span style="color: #008000">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">      <span style="color: #008000">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">         {write-host $job.PercentComplete <span style="color: #006080">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #008000">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       <span style="color: #008000">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          {write-host $successString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          <span style="color: #0000ff">return</span> $true}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorDescription"</span> $job.ErrorDescription</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">          <span style="color: #0000ff">return</span> $false}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMName = Read-Host <span style="color: #006080">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the name of the new snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$SnapshotName = Read-Host <span style="color: #006080">"Specify the name of the new snapshot"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">"ElementName='$VMName'"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># Take the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">$result = $VMMS.CreateVirtualSystemSnapshot($VM)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #008000"># Process the results</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">$snapshotSucceeded = ProcessResult $result <span style="color: #006080">"The snapshot was taken."</span> <span style="color: #006080">"Failed to take snapshot."</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">&#160;</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px"><span style="color: #008000"># If the snapshot is created - rename it</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px"><span style="color: #0000ff">if</span> ($snapshotSucceeded)</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">    {</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">    <span style="color: #008000"># Get the job object from the results</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">    $job2=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">    </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">    <span style="color: #008000"># Get the snapshot that is assocated with the completed job</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">    $newSnapshot = $job2.GetRelated(<span style="color: #006080">"Msvm_VirtualSystemSettingData"</span>) &#124; select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">    </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">    <span style="color: #008000"># Change the "ElementName" </span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">    $newSnapshot.ElementName = $SnapshotName</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">    </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">    <span style="color: #008000"># Apply the changes to the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">    $result2 = $VMMS.ModifyVirtualSystem($VM, $newSnapshot.GetText(1))</pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">    </pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">    <span style="color: #008000"># Check to see if renaming the snapshot succeeds</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: white;border-style: none;padding: 0px">    $renameSucceeded = ProcessResult $result2 <span style="color: #006080">"The new snapshot was renamed."</span> <span style="color: #006080">"Failed to rename the new snapshot."</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt;font-family: 'Courier New', courier, monospace;width: 100%;color: black;overflow: visible;direction: ltr;text-align: left;margin: 0em;line-height: 12pt;background-color: #f4f4f4;border-style: none;padding: 0px">    }</pre>
<!--CRLF--></div>
</div>
</div>
<p>The tricky part of this script is not actually taking the snapshot (which is done with <strong>CreateVirtualSystemSnapshot</strong>) but naming the snapshot after it is taken (Hyper-V always auto-generates a snapshot name for you &#8211; and you need to rename it if you want to have a custom snapshot name).&#160; Actually, that isn&#8217;t hard either &#8211; the hard bit is *finding* the snapshot after it is taken in order to name it.</p>
<p>The secret here is that the new snapshot object (in WMI) will actually be associated with the job object once the snapshot is complete.&#160; So this script waits for the snapshot job to complete, then it gets the related system setting data from the completed job, and uses that to rename the snapshot.&#160; Once you have figured this bit out &#8211; the rest is quite straight forward.</p>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225580" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float: none; margin: 0px; padding: 0px 0px 0px 0px;">
<script type="text/javascript">// < ![CDATA[
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/17/taking-a-snapshot-via-powershell-hyper-v.aspx'; 
// ]]&gt;</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
<br /><br /></div>
<p>This week I am going to be sharing a bunch of scripts that show you how to interact with virtual machine snapshots in Hyper-V.&nbsp; And I am going to start off with the hardest one &ndash; taking a snapshot:</p>
<div class="csharpcode">
<div id="codeSnippetWrapper" style="cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; overflow: auto; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 2000px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Function for handling WMI jobs / return values</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">Function ProcessResult($result, $successString, $failureString)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">{</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#Return success if the return value is "0"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   <span style="color: #0000ff;">if</span> ($result.ReturnValue <span style="color: #cc6633;">-eq</span> 0)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $successString} </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   <span style="color: #008000;">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">   ElseIf ($result.ReturnValue <span style="color: #cc6633;">-ne</span> 4096)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      {write-host $failureString <span style="color: #006080;">" Error value:"</span> $result.ReturnValue}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">   Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      {<span style="color: #008000;">#Get the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      $job=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">      <span style="color: #008000;">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">      <span style="color: #0000ff;">while</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 3 -or $job.JobState <span style="color: #cc6633;">-eq</span> 4)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">         {write-host $job.PercentComplete <span style="color: #006080;">"% complete"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          start-sleep 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #008000;">#Refresh the job object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          $job=[WMI]$result.job}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       <span style="color: #008000;">#A jobstate of "7" means success</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       <span style="color: #0000ff;">if</span> ($job.JobState <span style="color: #cc6633;">-eq</span> 7)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          {write-host $successString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          <span style="color: #0000ff;">return</span> $true}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">       Else</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          {write-host $failureString</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">          write-host <span style="color: #006080;">"ErrorDescription"</span> $job.ErrorDescription</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">          <span style="color: #0000ff;">return</span> $false}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">       }</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">}</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the Hyper-V Server to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$HyperVServer = Read-Host <span style="color: #006080;">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the virtual machine to use</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMName = Read-Host <span style="color: #006080;">"Specify the name of the virtual machine"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Prompt for the name of the new snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$SnapshotName = Read-Host <span style="color: #006080;">"Specify the name of the new snapshot"</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the management service</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"> </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Get the virtual machine object</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff;">filter</span> <span style="color: #006080;">"ElementName='$VMName'"</span> -namespace <span style="color: #006080;">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># Take the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">$result = $VMMS.CreateVirtualSystemSnapshot($VM)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #008000;"># Process the results</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">$snapshotSucceeded = ProcessResult $result <span style="color: #006080;">"The snapshot was taken."</span> <span style="color: #006080;">"Failed to take snapshot."</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">&nbsp;</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;"><span style="color: #008000;"># If the snapshot is created - rename it</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #0000ff;">if</span> ($snapshotSucceeded)</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">    {</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">    <span style="color: #008000;"># Get the job object from the results</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">    $job2=[WMI]$result.job</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">    </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">    <span style="color: #008000;"># Get the snapshot that is assocated with the completed job</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">    $newSnapshot = $job2.GetRelated(<span style="color: #006080;">"Msvm_VirtualSystemSettingData"</span>) | select -first 1</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">    </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">    <span style="color: #008000;"># Change the "ElementName" </span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">    $newSnapshot.ElementName = $SnapshotName</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">    </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">    <span style="color: #008000;"># Apply the changes to the snapshot</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">    $result2 = $VMMS.ModifyVirtualSystem($VM, $newSnapshot.GetText(1))</pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">    </pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">    <span style="color: #008000;"># Check to see if renaming the snapshot succeeds</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;">    $renameSucceeded = ProcessResult $result2 <span style="color: #006080;">"The new snapshot was renamed."</span> <span style="color: #006080;">"Failed to rename the new snapshot."</span></pre>
<!--CRLF-->
<pre style="font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; overflow: visible; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;">    }</pre>
<!--CRLF--></div>
</div>
</div>
<p>The tricky part of this script is not actually taking the snapshot (which is done with <strong>CreateVirtualSystemSnapshot</strong>) but naming the snapshot after it is taken (Hyper-V always auto-generates a snapshot name for you &ndash; and you need to rename it if you want to have a custom snapshot name).&nbsp; Actually, that isn&rsquo;t hard either &ndash; the hard bit is *finding* the snapshot after it is taken in order to name it.</p>
<p>The secret here is that the new snapshot object (in WMI) will actually be associated with the job object once the snapshot is complete.&nbsp; So this script waits for the snapshot job to complete, then it gets the related system setting data from the completed job, and uses that to rename the snapshot.&nbsp; Once you have figured this bit out &ndash; the rest is quite straight forward.</p>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10225580" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/10/17/taking-a-snapshot-via-powershell-hyper-v.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-22-55-80/TakeSnapshot.zip" length="1380" type="application/zip" />
		</item>
		<item>
		<title>Enabling processor compatibility with a script [Hyper-V]</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/01/20/enabling-processor-compatibility-with-a-script-hyper-v.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/01/20/enabling-processor-compatibility-with-a-script-hyper-v.aspx#comments</comments>
		<pubDate>Fri, 21 Jan 2011 07:26:00 +0000</pubDate>
		<dc:creator>Virtual PC Guy</dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Windows Server 2008 R2]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float:none;margin:0px;padding:0px 0px 0px 0px">


<br /><br /></div>
<p>When we released Windows Server 2008 R2 we added the ability to make a virtual machines processor more compatible for migration between different hardware platforms (details <a target="_blank" href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2009/06/09/processor-compatibility-in-hyper-v-r2.aspx">here</a>).&#160; Here is a PowerShell script to do this for you:</p>
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New', courier, monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: silver 1px solid;padding: 4px">
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">Function ProcessResult($result, $successString, $failureString)</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">{</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">   <span style="color: #008000">#Return success if the return value is "0"</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">      {write-host $successString} </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">   <span style="color: #008000">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">      {write-host $failureString <span style="color: #006080">"  Error value:"</span> $result.ReturnValue}</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">   Else</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">      {<span style="color: #008000">#Get the job object</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">      $job=[WMI]$result.job</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">      <span style="color: #008000">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">         {write-host $job.PercentComplete <span style="color: #006080">"% complete"</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">          start-sleep 1</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">          <span style="color: #008000">#Refresh the job object</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">          $job=[WMI]$result.job}</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">       <span style="color: #008000">#A jobstate of "7" means success</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">          {write-host $successString}</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">       Else</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">          {write-host $failureString</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">          write-host <span style="color: #006080">"ErrorDescription"</span> $job.ErrorDescription}</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">       }</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">}</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$VMName = Read-Host <span style="color: #006080">"Specify the name of the virtual machine"</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Get the management service</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">"ElementName='$VMName'"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># SettingType = 3 ensures that we do not get snapshots</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$SystemSettingData = $VM.getRelated(<span style="color: #006080">"Msvm_VirtualSystemSettingData"</span>) &#124; where {$_.SettingType <span style="color: #cc6633">-eq</span> 3}</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Get the processor setting data</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$ProcSetting = $SystemSettingData.getRelated(<span style="color: #006080">"Msvm_ProcessorSettingData"</span>) &#124; select -first 1</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Enable processor compatibility</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$ProcSetting.LimitProcessorFeatures = $true</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Apply the changes to the processor setting data back to the virtual machine</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$result = $VMMS.ModifyVirtualSystemResources($VM, $ProcSetting.GetText(1))</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"> </pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Process the result</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">ProcessResult $result <span style="color: #006080">"Enabled limited processor features."</span> <span style="color: #006080">"Failed to enable limited processor features."</span></pre>
<!--crlf--></div>
</div>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10118561" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float:none; margin:0px; padding:0px 0px 0px 0px;">
<script type="text/javascript"><!--
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/01/20/enabling-processor-compatibility-with-a-script-hyper-v.aspx'; 
// --></script>
<script src="http://tweetmeme.com/i/scripts/button.js" type="text/javascript"></script>
<br /><br /></div>
<p>When we released Windows Server 2008 R2 we added the ability to make a virtual machines processor more compatible for migration between different hardware platforms (details <a href="http://blogs.msdn.com/b/virtual_pc_guy/archive/2009/06/09/processor-compatibility-in-hyper-v-r2.aspx">here</a>).&nbsp; Here is a PowerShell script to do this for you:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 2000px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Function for handling WMI jobs / return values</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">Function ProcessResult($result, $successString, $failureString)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">{</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">   <span style="color: #008000">#Return success if the return value is "0"</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">   <span style="color: #0000ff">if</span> ($result.ReturnValue <span style="color: #cc6633">-eq</span> 0)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">      {write-host $successString} </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">   <span style="color: #008000">#If the return value is not "0" or "4096" then the operation failed</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">   ElseIf ($result.ReturnValue <span style="color: #cc6633">-ne</span> 4096)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">      {write-host $failureString <span style="color: #006080">"  Error value:"</span> $result.ReturnValue}</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">   Else</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">      {<span style="color: #008000">#Get the job object</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">      $job=[WMI]$result.job</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">      <span style="color: #008000">#Provide updates if the jobstate is "3" (starting) or "4" (running)</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">      <span style="color: #0000ff">while</span> ($job.JobState <span style="color: #cc6633">-eq</span> 3 -or $job.JobState <span style="color: #cc6633">-eq</span> 4)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">         {write-host $job.PercentComplete <span style="color: #006080">"% complete"</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">          start-sleep 1</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">          <span style="color: #008000">#Refresh the job object</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">          $job=[WMI]$result.job}</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">       <span style="color: #008000">#A jobstate of "7" means success</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">       <span style="color: #0000ff">if</span> ($job.JobState <span style="color: #cc6633">-eq</span> 7)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">          {write-host $successString}</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">       Else</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">          {write-host $failureString</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">          write-host <span style="color: #006080">"ErrorCode:"</span> $job.ErrorCode</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">          write-host <span style="color: #006080">"ErrorDescription"</span> $job.ErrorDescription}</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">       }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">}</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Prompt for the Hyper-V Server to use</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$HyperVServer = Read-Host <span style="color: #006080">"Specify the Hyper-V Server to use (enter '.' for the local computer)"</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Prompt for the virtual machine to use</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$VMName = Read-Host <span style="color: #006080">"Specify the name of the virtual machine"</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Get the management service</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Get the virtual machine object</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$VM = gwmi MSVM_ComputerSystem -<span style="color: #0000ff">filter</span> <span style="color: #006080">"ElementName='$VMName'"</span> -namespace <span style="color: #006080">"root\virtualization"</span> -computername $HyperVServer</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># SettingType = 3 ensures that we do not get snapshots</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$SystemSettingData = $VM.getRelated(<span style="color: #006080">"Msvm_VirtualSystemSettingData"</span>) | where {$_.SettingType <span style="color: #cc6633">-eq</span> 3}</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Get the processor setting data</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$ProcSetting = $SystemSettingData.getRelated(<span style="color: #006080">"Msvm_ProcessorSettingData"</span>) | select -first 1</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Enable processor compatibility</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$ProcSetting.LimitProcessorFeatures = $true</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Apply the changes to the processor setting data back to the virtual machine</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$result = $VMMS.ModifyVirtualSystemResources($VM, $ProcSetting.GetText(1))</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"> </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Process the result</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">ProcessResult $result <span style="color: #006080">"Enabled limited processor features."</span> <span style="color: #006080">"Failed to enable limited processor features."</span></pre>
<!--crlf--></div>
</div>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10118561" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/01/20/enabling-processor-compatibility-with-a-script-hyper-v.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Components-PostAttachments/00-10-11-85-61/ProcessorCompatibility.zip" length="1309" type="application/x-zip-compressed" />
		</item>
		<item>
		<title>Script to attach a USB device to a virtual machine [VPC]</title>
		<link>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/01/18/script-to-attach-a-usb-device-to-a-virtual-machine-vpc.aspx</link>
		<comments>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/01/18/script-to-attach-a-usb-device-to-a-virtual-machine-vpc.aspx#comments</comments>
		<pubDate>Wed, 19 Jan 2011 07:29:00 +0000</pubDate>
		<dc:creator>Virtual PC Guy</dc:creator>
				<category><![CDATA[Hyper-V]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Windows Virtual PC]]></category>
		<category><![CDATA[Developing on Windows Virtual PC / Virtual Server / Hyper-V]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div class="wlWriterHeaderFooter" style="float:none;margin:0px;padding:0px 0px 0px 0px">


<br /><br /></div>
<p>A couple of people have asked me how to automate attaching a USB device to a Windows Virtual PC virtual machine, so here is a PowerShell script to do just that:</p>
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New', courier, monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: silver 1px solid;padding: 4px">
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Connect to Virtual PC</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$vpc = new-object -com VirtualPC.Application</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">&#160;</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Get VM name</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$vmName = Read-host <span style="color: #006080">"Specify the name of the virtual machine that you want to use"</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">&#160;</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># List available USB devices</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">write-host <span style="color: #006080">"The following USB devices are available:"</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$vpc.USBDeviceCollection &#124; select -ExpandProperty DeviceString</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">&#160;</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Get the USB device name</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$usb = Read-host <span style="color: #006080">"Enter the name of the USB device that you want to connect to the virtual machine"</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">&#160;</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Get the VM object</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$vm = $vpc.findVirtualMachine($vmName)</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">&#160;</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Get the USB object</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$usbDevice = $vpc.USBDeviceCollection &#124; ? {$_.DeviceString <span style="color: #cc6633">-eq</span> $usb} &#124; select -first 1</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">&#160;</pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px"><span style="color: #008000"># Attach the device - this will fail if the VM is not running</span></pre>
<!--crlf-->
<pre style="text-align: left;line-height: 12pt;background-color: white;margin: 0em;width: 100%;font-family: 'Courier New', courier, monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible;border-style: none;padding: 0px">$vm.AttachUSBDevice($usbDevice)</pre>
<!--crlf--></div>
</div>
<p>Note that I am using the &#8220;DeviceString&#8221; of the USB device &#8211; which is not guaranteed to be unique.&#160; So what I do is just grab the first one that matches.</p>
<p>Cheers, <br />Ben</p><div style="clear:both"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10117488" width="1" height="1"/>]]></description>
			<content:encoded><![CDATA[<div class="wlWriterHeaderFooter" style="float:none; margin:0px; padding:0px 0px 0px 0px;">
<script type="text/javascript"><!--
 tweetmeme_url = 'http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/01/18/script-to-attach-a-usb-device-to-a-virtual-machine-vpc.aspx'; 
// --></script>
<script src="http://tweetmeme.com/i/scripts/button.js" type="text/javascript"></script>
<br /><br /></div>
<p>A couple of people have asked me how to automate attaching a USB device to a Windows Virtual PC virtual machine, so here is a PowerShell script to do just that:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Connect to Virtual PC</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$vpc = new-object -com VirtualPC.Application</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">&nbsp;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Get VM name</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$vmName = Read-host <span style="color: #006080">"Specify the name of the virtual machine that you want to use"</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">&nbsp;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># List available USB devices</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">write-host <span style="color: #006080">"The following USB devices are available:"</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$vpc.USBDeviceCollection | select -ExpandProperty DeviceString</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">&nbsp;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Get the USB device name</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$usb = Read-host <span style="color: #006080">"Enter the name of the USB device that you want to connect to the virtual machine"</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">&nbsp;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Get the VM object</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$vm = $vpc.findVirtualMachine($vmName)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">&nbsp;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Get the USB object</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$usbDevice = $vpc.USBDeviceCollection | ? {$_.DeviceString <span style="color: #cc6633">-eq</span> $usb} | select -first 1</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">&nbsp;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #008000"># Attach the device - this will fail if the VM is not running</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">$vm.AttachUSBDevice($usbDevice)</pre>
<!--crlf--></div>
</div>
<p>Note that I am using the &ldquo;DeviceString&rdquo; of the USB device &ndash; which is not guaranteed to be unique.&nbsp; So what I do is just grab the first one that matches.</p>
<p>Cheers, <br />Ben</p><div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10117488" width="1" height="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.msdn.com/b/virtual_pc_guy/archive/2011/01/18/script-to-attach-a-usb-device-to-a-virtual-machine-vpc.aspx/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Components-PostAttachments/00-10-11-74-88/AttachUSBDevice.zip" length="630" type="application/x-zip-compressed" />
		</item>
	</channel>
</rss>

