4

当我运行以下代码时

ConnectionOptions options = new ConnectionOptions();
        options.EnablePrivileges = true;
        options.Impersonation = ImpersonationLevel.Impersonate;
        options.Authentication = AuthenticationLevel.Packet;
        options.Authority = "ntdlmdomain:InsTIL.com";
        options.Username = "instil" + @"\" + "admin";
        options.Password = "Pwd";

        ManagementScope scope= new ManagementScope(string.Format(@"\\172.16.2.171\root\cimv2"),options);
        scope.Connect();
        if (scope.IsConnected == true)
        {
           Console.WriteLine("Connection Succeeded");
        }
        else
        {
            Console.WriteLine("Connection Failed");
        }


        ObjectQuery query = new ObjectQuery("Select * from win32_operatingsystem");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

        ManagementObjectCollection querycollection = searcher.Get();
        foreach (ManagementObject mobj in querycollection)
        {
            Console.WriteLine("Computer name: {0}", mobj["csname"]);
            Console.WriteLine("Windows Directory : {0}", mobj["WindowsDirectory"]);
            Console.WriteLine("Operating System: {0}", mobj["Caption"]);
            Console.WriteLine("Version: {0}", mobj["Version"]);
            Console.WriteLine("Manufacturer : {0}", mobj["Manufacturer"]);
        }

我收到以下错误

  Unhandled Exception: System.Management.ManagementException: Invalid parameter
   at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
    at System.Management.ManagementScope.InitializeGuts(Object o)
   at System.Management.ManagementScope.Initialize()
  at remote_wmi.Program.Main(String[] args) in E:\.net prep\.net examples\remote wmi\remote wmi\Program.cs:line 21

请帮我知道为什么会这样。?这段代码有什么问题?在执行这段代码之前我们需要做任何事情吗?如果是,请说明。

提前致谢。

4

1 回答 1

7

我做了以下更改,然后我得到了答案。

1)我没有用用户名指定域名。 options.Username = "admin";代替 options.Username = "instil" + @"\" + "admin";

2) options.Authority = "ntlmdomain:InsTIL.com";代替options.Authority = "ntdlmdomain:InsTIL.com";

于 2013-03-19T10:34:47.447 回答