Freigeben über


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

Comments

  • Anonymous
    October 02, 2008
    The comment has been removed

  • Anonymous
    January 16, 2009
    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 rootvirtualization -Query "Select * From Msvm_ComputerSystem where ElementName='<VM_NAME>'"
    $vm.RequestStateChange(2) -Taylor

  • Anonymous
    January 14, 2010
    Hi Justin, I'm looking for a way to force shutdown the VM like if it was powered off (Turn Off option from HyperV Manager).. THe only thing i was able to do so far was to request the shutdown like u mentioned above (in different ways).. Would u know how to acomplish that?