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

5
当我运行以下代码时
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

请帮我了解为什么会出现这种情况?这段代码有什么问题吗?在执行此代码之前需要做些什么吗?如果需要,请指明。

提前致谢。


第21行是哪一行? - PhonicUK
@phonicUK:scope.connect(); - Gomathipriya
服务帐户是网络服务类型吗? - Darren
@DarrenDavies:对不起,我不知道如何检查那个。 - Gomathipriya
1个回答

8
我进行了以下更改,然后得到了答案。
1)我没有在用户名中指定域名。 options.Username = "admin"; 而不是 options.Username = "instil" + @"\" + "admin"; 2) options.Authority = "ntlmdomain:InsTIL.com"; 而不是 options.Authority = "ntdlmdomain:InsTIL.com";

1
我也遇到了“ntdlmdomain”问题。微软还没有在他们的API文档中修复这个错别字。 - Chad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接