【问题标题】:List domain users with wmi使用 wmi 列出域用户
【发布时间】:2014-01-21 20:13:18
【问题描述】:

我想在 C# 中使用 WMI 列出 Windows 域的所有用户。

有人可以帮我吗?

这是我的代码:

try
            {
                ConnectionOptions connection = new ConnectionOptions();
                connection.Username = user;
                connection.Authority = "ntlmdomain:" + domain;
                connection.Password = pwd;

                SelectQuery query = new SelectQuery("SELECT * FROM Win32_UserAccount");
                ManagementScope scope = new ManagementScope(@"\\FullComputerName\\root\\CIMV2", connection);
                scope.Connect();
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("Account Type: " + queryObj["AccountType"]);
                    Console.WriteLine("Caption: " + queryObj["Caption"]);
                    Console.WriteLine("Description: " + queryObj["Description"]);
                    Console.WriteLine("Disabled: " + queryObj["Disabled"]);
                    Console.WriteLine("Domain: " + queryObj["Domain"]);
                    Console.WriteLine("Full Name: " + queryObj["FullName"]);
                    Console.WriteLine("Local Account: " + queryObj["LocalAccount"]);
                    Console.WriteLine("Lockout: " + queryObj["Lockout"]);
                    Console.WriteLine("Name: " + queryObj["Name"].ToString());
                    Console.WriteLine("Password Changeable: " + queryObj["PasswordChangeable"]);
                    Console.WriteLine("Password Expires: " + queryObj["PasswordExpires"]);
                    Console.WriteLine("Password Required: " + queryObj["PasswordRequired"]);
                    Console.WriteLine("SID: " + queryObj["SID"]);
                    Console.WriteLine("SID Type: " + queryObj["SIDType"]);
                    Console.WriteLine("Status: " + queryObj["Status"]);
                    Console.WriteLine("");
                }
            }
            catch (ManagementException err)
            {
                Console.WriteLine("An error occured while querying for WMI data: " + err.Message);
            }
            catch (System.UnauthorizedAccessException unauthorizedErr)
            {
                Console.WriteLine("Connection error " + "(user name or password might be incorrect): " + unauthorizedErr.Message);
            }

【问题讨论】:

  • 为什么是 C? Powershell 执行此操作应该没什么问题...
  • 好的,那么当你运行它时会发生什么?比如,有什么不好的?您已经尝试过什么来追踪/纠正问题?
  • 如果我运行它,它会说它的参数错误

标签: windows dns c# wmi


【解决方案1】:

ManagementScope 构造函数中的命名空间路径中有错字:

ManagementScope scope = new ManagementScope(@"\\FullComputerName\\root\\CIMV2", connection);

字符串应该是@"\\FullComputerName\root\CIMV2""\\\\FullComputerName\\root\\CIMV2"

请注意,您不能为本地连接指定用户帐户。因此,如果FullComputerName 是本地计算机,请改用它:

ManagementScope scope = new ManagementScope("root\\CIMV2");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2014-05-10
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多