WMI with Visual Studio - Access is denied

workinprogress 20 Reputation points
2023-09-07T09:58:21.1766667+00:00

Hi I am a newbie with WMI in C#. I am trying to connect to a virtual machine with Operating system Windows 10 from a machine with OS Windows 10. I can ping the machine, also I can start a process on the VM from my machine, for example Windows Application Driver. By contrast, when I attempt to execute the following Program I am refused with System.UnauthorizedAccessException: Access is denied. I need WMI to identify if a particular process is running on the VM. The code is the following:

The code in the Main() method is the following (of course username and password are not the ones in the code snippet):

            ConnectionOptions connectoptions = new ConnectionOptions();
            connectoptions.Username = @"domain\username";
            connectoptions.Password = "password";
            ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2");
            scope.Options = connectoptions;
            SelectQuery query = new SelectQuery("select * from Win32_Process");
            string WindowHandle = string.Empty;
            using (ManagementObjectSearcher searcher = new
                        ManagementObjectSearcher(scope, query))
            {
                ManagementObjectCollection collection = searcher.Get();
                foreach (ManagementObject process in collection)
                {
                    Console.WriteLine(process.ClassPath);
                    foreach (PropertyData prop in process.Properties)
                    {
                        Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
                    }
                }
            }

I followed the steps in the answer https://social.msdn.microsoft.com/Forums/en-US/b31a3fd1-cdf8-46ff-8337-89dd96976780/wmi-access-denied?forum=winservergen

but no luck.

Thanks and regards

Windows for business Windows Client for IT Pros User experience Remote desktop services and terminal services
Windows for business Windows Client for IT Pros User experience Other
Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2023-09-08T13:58:09.6+00:00

    Hi @workinprogress ,Welcome to Microsoft Q&A,

    So the issues were the DCOM settings on both the virtual machine and the local machine.

    You need ensure remote access and operations are allowed for the user.

    You needed to configure permissions on the virtual machine using the dcomcnfg tool.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.