Hyper-V WMI - C# samples?


  • Are there any C# samples for the new Hyper-V management interfaces released a few days ago at


    Wednesday, January 30, 2008 3:32 AM


All replies

  • Not at the moment.




    Wednesday, January 30, 2008 7:15 PM
  • You can check my blog http://dungkhoang/

    I have some examples in PowerShell that you can easily adapt to C#




    Saturday, February 9, 2008 5:11 PM
  • I have some code borrowed from the MSDN SDK site which simply lists the virtual machines registered on the Hyper-V hypervisor.  The code works fine in a standalone C# program but when I try to use it inside a Web Service it doesn't list any VMs, just the Host system.

    This problem is seen regardless of publishing the Web Service via IIS or the VS integrated debug web server.

                ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem"); 
                ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(mVirtScope, queryObj); 
                    ManagementObjectCollection vmCollection = vmSearcher.Get(); 
                    int count = 0
                    VirtualMachine[] vms = new VirtualMachine[vmCollection.Count]; 
                    foreach (ManagementObject vm in vmCollection) 
                        vms[count++] = new VirtualMachine(vm["Name"].ToString(), vm["ElementName"].ToString()); 
                    return vms; 
                catch (Exception e) 
                    Console.WriteLine("Exception " + e.ToString()); 

    • Proposed as answer by dms997 Friday, May 27, 2016 2:11 AM
    Tuesday, February 10, 2009 3:45 PM
  • I just hit this as well. 

    My "solution" was to add a new AppPool in IIS running as LocalSystem.

    Then, I switched the Application Pool setting for the webservice I had published to IIS, and I can get it to query the VMs properly.  Before it would return the primary VM (physical host) and no other VM names.  After making that switch all of the VMs on my machine are now returned.

    There is likely a better way, but it will work for the purposes of lab automation. :)
    Monday, February 23, 2009 12:49 PM