Hyper-V WMI Using PowerShell - Part 4 and Negative 1

Hyper-V WMI Using PowerShell - Part 4 and Negative 1

  • Comments 5
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Taylor,

    These Hyper-V management with Powershell scripting blogs are great and I am finding it really useful for setting up Hyper-V environments from scratch. One question though, is it possible to perform the tasks synchronously? My preference when creating VHDs is to create fixed size disks and allow them to build entirely before creating the next one in order to minimise fragmentation.

    Thanks

    Rob

  • Rob,

    In short yes and no...  Yes in that you can wait for the WMI job to compleate prior to starting the next job and No in that the API its self does not support this.

    Here's the code to wait for a job to be compleate:

    while (([WMI]$Job.Job.JobState -eq 2) -or ([WMI]$Job.Job.JobState -eq 3) -or ([WMI]$Job.Job.JobState -eq 4)) {Start-Sleep -m 100}

  • Ok.  I can't find any site to do this, but how do you do these scripts?  I tried copy/paste into powershell.  I tried creating a .wmi file.  That didn't work.  Any help would be appreciated.  Thanks.

  • Taylor Brown del team di sviluppo di Hyper-V ha fatto 4 post abbastanza interessanti e introduttivi sull'uso

  • Kirk,

    There are two option's

      1) Copy each line and run them on there own in a PowerShell Window

      2) Create a .ps1 file with the contents in it, to execute it you will need to configure the execution policy (see Get-Help Set-ExecutionPolicy).

    -Enjoy