Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there,

I am trying to get ram, cpu and disk data from windows server 2012 with a sample code like below;

private static void Main(string[] args)
        {
            //can you add try catch to this code
            try
            {
                string vmName = "windows1";  // Replace with your VM name
                ManagementScope scope = new ManagementScope(@"\\.\ROOT\virtualization\v2");

                // Query for the virtual machine
                ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM Msvm_ComputerSystem WHERE ElementName = '{0}'", vmName));

                // Execute the query
                using ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    // Get the memory
                    Console.WriteLine("Memory: {0}", queryObj["TotalMemory"]);

                    // Get the disks
                    using ManagementObjectCollection relatedDisks = queryObj.GetRelated("Msvm_VirtualHardDisk");
                    foreach (ManagementObject disk in relatedDisks)
                    {
                        Console.WriteLine("Disk path: {0}", disk["Path"]);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }


What I have tried:

But I am getting invalid namespace, how can I fix this?
Posted
Updated 13-Jul-23 8:08am

1 solution

It's telling you the namespace "virtualization\V2" doesn't exist on the machine. You don't have the Hyper-V WMI provider subsystem installed. Go into "Turn Windows Features on or off" to see what you're missing under Hyper-V.
 
Share this answer
 
Comments
AstroTheDog 13-Jul-23 14:10pm    
Hi Dave,

Thanks for your reply. I checked services there are a couple of Hyper-V services that I tried to start but they did not start. And I didn't see any Hyper-V manager tool on the programs as well. How can I fix this problem?

Best regards.
Dave Kreskowiak 13-Jul-23 14:22pm    
You didn't do what I told you. "Services" is not "Turn Windows Features on or off". Type "Features" into the Window Search bar to find it.
AstroTheDog 14-Jul-23 2:46am    
I got; Hyper-V cannot be installed a hypervisor is already running. I think this may work for me; https://www.youtube.com/watch?v=fwO17rgx_m4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900