远程WMI连接C# - 无效的参数错误

问题描述:

当我运行下面的代码远程WMI连接C# - 无效的参数错误

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 

请帮我知道为什么它的发生?这段代码有什么问题?我们是否需要在执行此代码之前做任何事情?如果是,请指定它。

在此先感谢。

+0

哪条线是线21? – PhonicUK 2013-03-19 10:12:10

+0

@phonicUK:scope.connect(); – Gomathipriya 2013-03-19 10:13:26

+0

是类型网络服务的服务帐户吗? – 2013-03-19 10:15:43

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

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

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

+1

我也受到了'ntdlmdomain'问题的困扰。微软还没有解决他们的API文档中的错字。 – Chad 2014-03-14 16:23:16