jscarle/HyperV.NET

Fetching list of Virtual Machine in the host name

Closed this issue · 1 comments

Is there any feature to fetch the list of virtual machines available in the current host machine?

I was able to fetch the list of VMs from the primary machine using the WMI APIs

private ManagementObjectCollection GetVirtualMachines()
{
    string vmHost = Dns.GetHostName();
    string wmiNamespace = @"\\" + vmHost + @"\root\virtualization\v2";
    string fetchVmDetailsQuery = @"SELECT * FROM Msvm_ComputerSystem WHERE Caption = 'Virtual Machine' AND ElementName = '" + vmName + @"'";

    ManagementScope virtualizationScope = new ManagementScope(wmiNamespace);
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(virtualizationScope, new ObjectQuery(fetchVmDetailsQuery)))
    using (ManagementObjectCollection collection = searcher.Get())
    {
        return collection;
    }
}