page hit counter
Welcome to MSDN Blogs Sign in | Join | Help

Taylor Brown's Blog

Test Lead for Windows Core OS Division on the Hyper-V Team.

Syndication

News

Welcome to the professional blog of Taylor Brown.  I am a test lead on the core virtualization team (Hyper-V) at Microsoft.

This blog will contain information about virtualization, Microsoft, Hyper-V, operating systems, testing, and whatever else I end up talking about...

 

Standard Microsoft Disclaimer:
"This posting is provided "AS IS" with no warranties, and confers no rights. You assume all risk for your use."

-Taylor Brown


Hyper-V WMI Using PowerShell - Part 4 and Negative 1 (Shutdown IC Powershell/C#)

I hope everyone is enjoying Hyper-V RC1 (yep I just linked to my own post -shamless maybe) but, as promissed here's some more WMI goodness.   

Title got your attention?  Well this is a two part post…  First I will show how to use the Shutdown IC to initiate a shutdown of a guest using PowerShell (that's part 4)  Ok so why Negative 1?  Well what did we do before PowerShell (yeah yeah vbscript but go with me) one option was C# so Part Negative 1 is a C# Example…

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.  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.  It really is that simple.

$Vm = Get-WmiObject -Namespace root\virtualization  -Query "Select * From Msvm_ComputerSystem Where ElementName='Vista'"


$ShutdownIC = Get-WmiObject -Namespace root\virtualization  -Query "Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_ShutdownComponent"


$ShutdownIC.InitiateShutdown("TRUE", "Need to shutdown")

And here’s the same code in C#…

using System;
using System.Collections;
using System.Text;
using System.Management;


namespace ShutdownViaIC
{
    class Program
    {
        static void Main(string[] args)
        {
            //Connect to the Remote Machines Management Scope
            ConnectionOptions options =  new ConnectionOptions();
            ManagementScope scope = new ManagementScope(@"\\localhost\root\virtualization"); scope.Connect();

            //Get the msvm_computersystem for the given VM (Vista)         
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,
                new ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = 'Vista'"));

            //Select the first object in the Searcher collection
            IEnumerator enumr = searcher.Get().GetEnumerator();
            enumr.MoveNext();
            ManagementObject msvm_computersystem = (ManagementObject)(enumr.Current);

            //Use the association to get the msvm_shutdowncomponent for the msvm_computersystem
            ManagementObjectCollection collection = msvm_computersystem.GetRelated("Msvm_ShutdownComponent");
            ManagementObjectCollection.ManagementObjectEnumerator enumerator = collection.GetEnumerator();
            enumerator.MoveNext();
            ManagementObject msvm_shutdowncomponent = (ManagementObject)enumerator.Current;

            //Get the InitiateShudown Parameters
            ManagementBaseObject inParams = msvm_shutdowncomponent.GetMethodParameters("InitiateShutdown");
            inParams["Force"] = true;
            inParams["Reason"] = "Need to Shutdown";

            //Invoke the Method
            ManagementBaseObject outParams = msvm_shutdowncomponent.InvokeMethod("InitiateShutdown", inParams, null);
            uint returnValue = (uint)outParams["ReturnValue"];

            //Zero indicates success
            if (returnValue != 0)
                Console.WriteLine("SHUTDOWN Failed");            
        }
    }
}

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…  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).

--Taylor Brown
--Hyper-V test team

Published Wednesday, May 21, 2008 7:00 AM by taylorb

Filed under: , , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Hyper-V WMI Using PowerShell - Part 4 and Negative 1 (Shutdown IC Powershell/C#) @ Thursday, October 02, 2008 11:08 AM

So is it possible to issue a shutdown command that also install updates as well?

 ===============================
Richard,
Currently the shutdown intergration component dosn't support that functionaliity but I can see that would be usefull - I will submit a feature request on your behalf...

-Taylor

Richard Whitcher

# re: Hyper-V WMI Using PowerShell - Part 4 and Negative 1 (Shutdown IC Powershell/C#) @ Friday, January 16, 2009 4:12 PM

How would I modify the powershell script to start a vm instead of shutting it down? I am having a difficult time finding anything on starting a vm with powershell and wmi.

========================================================
Justin,
Try this:

Get-WmiObject -Namespace root\virtualization -Query "Select * From Msvm_ComputerSystem where ElementName='<VM_NAME>'"
$vm.RequestStateChange(2)

-Taylor

Justin

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
Page view tracker