<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Windows Virtualization Team Blog : WMI, virtualization management</title><link>http://blogs.technet.com/virtualization/archive/tags/WMI/virtualization+management/default.aspx</link><description>Tags: WMI, virtualization management</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Hyper-V WMI Using PowerShell - Part 4 and Negative 1</title><link>http://blogs.technet.com/virtualization/archive/2008/05/21/hyper-v-wmi-using-powershell-part-4-and-negative-1.aspx</link><pubDate>Wed, 21 May 2008 10:10:00 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3057646</guid><dc:creator>Taylorb</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.technet.com/virtualization/comments/3057646.aspx</comments><wfw:commentRss>http://blogs.technet.com/virtualization/commentrss.aspx?PostID=3057646</wfw:commentRss><description>&lt;P&gt;I hope&amp;nbsp;everyone is enjoying&amp;nbsp;&lt;A class="" href="http://blogs.technet.com/virtualization/archive/2008/05/20/hyper-v-rc1-release-available-on-microsoft-download-center.aspx" target=_blank mce_href="http://blogs.technet.com/virtualization/archive/2008/05/20/hyper-v-rc1-release-available-on-microsoft-download-center.aspx"&gt;Hyper-V RC1&lt;/A&gt;&amp;nbsp;&lt;EM&gt;(yep I just linked to my own post -shamless maybe) &lt;/EM&gt;but, as promissed here's&amp;nbsp;some more WMI goodness.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Title got your attention?&amp;nbsp; Well&amp;nbsp;this is a two part post…&amp;nbsp; First I will show how to use the Shutdown IC to initiate a shutdown of a guest using&amp;nbsp;PowerShell (that's part 4)&amp;nbsp; Ok so why Negative 1?&amp;nbsp; Well what did we do before PowerShell &lt;EM&gt;(yeah yeah vbscript but go with me)&lt;/EM&gt; one option was C# so Part Negative 1 is a C# Example… &lt;/P&gt;
&lt;P&gt;Here’s the PowerShell to shutdown a VM named “Vista”… Just like previous examples, we get the Msvm_ComputerSystem… then we use the Associators of query to get the Msvm_ShutdownComponent associated with that VM.&amp;nbsp; Then we just call InitiateShutdown, the first parameter is wether to force a shutdown (like running shutdown /f inside the virtual machine) and the second is the reason for the shutdown.&amp;nbsp; It really is that simple.&lt;/P&gt;
&lt;TABLE class="" cellSpacing=0 cellPadding=0 width="95%" bgColor=#fafafa border=2&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top&gt;
&lt;P&gt;&lt;FONT face="Courier New" color=#000000 size=3&gt;$Vm = Get-WmiObject -Namespace root\virtualization&amp;nbsp; -Query "Select * From Msvm_ComputerSystem Where ElementName='Vista'"&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face="Courier New" color=#000000 size=3&gt;
&lt;P&gt;&lt;BR&gt;$ShutdownIC = Get-WmiObject -Namespace root\virtualization&amp;nbsp; -Query "Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_ShutdownComponent"&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;$ShutdownIC.InitiateShutdown("TRUE", "Need to shutdown")&lt;/P&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;And here’s the same code in C#…&lt;/P&gt;
&lt;DIV&gt;&lt;PRE style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 8pt; PADDING-BOTTOM: 0px; MARGIN: 0em; OVERFLOW: visible; WIDTH: 100%; COLOR: black; BORDER-TOP-STYLE: none; LINE-HEIGHT: 12pt; PADDING-TOP: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; BORDER-BOTTOM-STYLE: none"&gt;&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System;
&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Collections;
&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Text;
&lt;SPAN style="COLOR: #0000ff"&gt;using&lt;/SPAN&gt; System.Management;


&lt;SPAN style="COLOR: #0000ff"&gt;namespace&lt;/SPAN&gt; ShutdownViaIC
{
    &lt;SPAN style="COLOR: #0000ff"&gt;class&lt;/SPAN&gt; Program
    {
        &lt;SPAN style="COLOR: #0000ff"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;void&lt;/SPAN&gt; Main(&lt;SPAN style="COLOR: #0000ff"&gt;string&lt;/SPAN&gt;[] args)
        {
            &lt;SPAN style="COLOR: #008000"&gt;//Connect to the Remote Machines Management Scope&lt;/SPAN&gt;
            ConnectionOptions options =  &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ConnectionOptions();
            ManagementScope scope = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ManagementScope(&lt;SPAN style="COLOR: #006080"&gt;@"\\localhost\root\virtualization"&lt;/SPAN&gt;); scope.Connect();

            &lt;SPAN style="COLOR: #008000"&gt;//Get the msvm_computersystem for the given VM (Vista)         &lt;/SPAN&gt;
            ManagementObjectSearcher searcher = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ManagementObjectSearcher(scope,
                &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; ObjectQuery(&lt;SPAN style="COLOR: #006080"&gt;"SELECT * FROM Msvm_ComputerSystem WHERE ElementName = 'Vista'"&lt;/SPAN&gt;));

            &lt;SPAN style="COLOR: #008000"&gt;//Select the first object in the Searcher collection&lt;/SPAN&gt;
            IEnumerator enumr = searcher.Get().GetEnumerator();
            enumr.MoveNext();
            ManagementObject msvm_computersystem = (ManagementObject)(enumr.Current);

            &lt;SPAN style="COLOR: #008000"&gt;//Use the association to get the msvm_shutdowncomponent for the msvm_computersystem&lt;/SPAN&gt;
            ManagementObjectCollection collection = msvm_computersystem.GetRelated(&lt;SPAN style="COLOR: #006080"&gt;"Msvm_ShutdownComponent"&lt;/SPAN&gt;);
            ManagementObjectCollection.ManagementObjectEnumerator enumerator = collection.GetEnumerator();
            enumerator.MoveNext();
            ManagementObject msvm_shutdowncomponent = (ManagementObject)enumerator.Current;

            &lt;SPAN style="COLOR: #008000"&gt;//Get the InitiateShudown Parameters&lt;/SPAN&gt;
            ManagementBaseObject inParams = msvm_shutdowncomponent.GetMethodParameters(&lt;SPAN style="COLOR: #006080"&gt;"InitiateShutdown"&lt;/SPAN&gt;);
            inParams[&lt;SPAN style="COLOR: #006080"&gt;"Force"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: #0000ff"&gt;true&lt;/SPAN&gt;;
            inParams[&lt;SPAN style="COLOR: #006080"&gt;"Reason"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: #006080"&gt;"Need to Shutdown"&lt;/SPAN&gt;;

            &lt;SPAN style="COLOR: #008000"&gt;//Invoke the Method&lt;/SPAN&gt;
            ManagementBaseObject outParams = msvm_shutdowncomponent.InvokeMethod(&lt;SPAN style="COLOR: #006080"&gt;"InitiateShutdown"&lt;/SPAN&gt;, inParams, &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;);
            &lt;SPAN style="COLOR: #0000ff"&gt;uint&lt;/SPAN&gt; returnValue = (&lt;SPAN style="COLOR: #0000ff"&gt;uint&lt;/SPAN&gt;)outParams[&lt;SPAN style="COLOR: #006080"&gt;"ReturnValue"&lt;/SPAN&gt;];

            &lt;SPAN style="COLOR: #008000"&gt;//Zero indicates success&lt;/SPAN&gt;
            &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (returnValue != 0)
                Console.WriteLine(&lt;SPAN style="COLOR: #006080"&gt;"SHUTDOWN Failed"&lt;/SPAN&gt;);            
        }
    }
}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;PowerShell like most scripting languages can do simple tasks well simply, but your power and control is limited… This is still the case with PowerShell the code to initiate this shutdown is dead simple in PowerShell and all of three lines, you have more ‘using’ lines than that in the C# code…&amp;nbsp; However when it comes to writing more complex solutions like a Windows service that monitors Hyper-V and implements some custom business logic C# is a great choice… (it’s what the Hyper-V UI is written in).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;--Taylor Brown &lt;BR&gt;--Hyper-V test team&lt;/STRONG&gt;&lt;/P&gt;&lt;!-- Start of StatCounter Code --&gt;
&lt;SCRIPT type=text/javascript&gt;
sc_project=3725534; 
sc_invisible=1; 
sc_partition=45; 
sc_security="9a688012"; 
&lt;/SCRIPT&gt;

&lt;SCRIPT src="http://www.statcounter.com/counter/counter_xhtml.js" type=text/javascript&gt;&lt;/SCRIPT&gt;
&lt;NOSCRIPT&gt;&lt;/NOSCRIPT&gt;&lt;!-- End of StatCounter Code --&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3057646" width="1" height="1"&gt;</description><category domain="http://blogs.technet.com/virtualization/archive/tags/Hyper-V/default.aspx">Hyper-V</category><category domain="http://blogs.technet.com/virtualization/archive/tags/virtualization+management/default.aspx">virtualization management</category><category domain="http://blogs.technet.com/virtualization/archive/tags/WMI/default.aspx">WMI</category></item></channel></rss>