pslover
pslover

Reputation: 72

WMI connection error C#

using System;
using System.Management;

public class Class1
{
    public static void Main()
    { 
        string strComputer = string.Format(@"machineName.domainname\root\cimv2");
        ConnectionOptions options = new ConnectionOptions();
        options.EnablePrivileges = true;
        options.Impersonation = ImpersonationLevel.Impersonate;
        options.Authentication = AuthenticationLevel.Packet;
        options.Authority = "ntlmdomain:InsTIL.com:InsTIL.com";
        options.Username = "usr";
        options.Password = "pwd";

        ManagementScope oMs = new ManagementScope(strComputer, options);

        SelectQuery query =new SelectQuery("Select * From Win32_Directory Where Name ='"+string.Format(@"C:\Scripts")+"'");
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,query);
        ManagementObjectCollection oReturnCollection = oSearcher.Get();

        if (oReturnCollection.Count < 1)
        {
            Console.WriteLine("Folder does not exist");
        }
        else
        {
            Console.WriteLine("Folder does exist");
        }

    }
}

I'm trying to connect to remote machine and checking existence of folder.But I'm getting below mentioned error.

I tried and incorporated changes discussed in remote wmi connection c# - invalid parameter error

Program abruptly stops working and throws below error:

Unhandled Exception: System.Management.ManagementException: Invalid parameter
   at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
   at System.Management.ManagementPath.CreateWbemPath(String path)
   at System.Management.ManagementPath..ctor(String path)
   at Class1.Main()

Upvotes: 1

Views: 932

Answers (1)

Jon Tirjan
Jon Tirjan

Reputation: 3694

You need backslashes before your machine name. Change this:

string strComputer = string.Format(@"machineName.domainname\root\cimv2");

to this:

string strComputer = string.Format(@"\\machineName.domainname\root\cimv2");

Upvotes: 1

Related Questions