zoukankan      html  css  js  c++  java
  • WMI远程添加修改用户

    /// <summary>
        /// WMI用户操作
        /// </summary>
        public class Win32_UserAccount
        {
            private string ip = string.Empty;
            public string IP
            {
                get { return ip; }
                set { ip = value; }
            }

            private string username = string.Empty;
            public string UserName
            {
                get { return username; }
                set { username = value; }
            }

            private string password;

            public string PassWord
            {
                get { return password; }
                set { password = value; }
            }

            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="_ip">目标IP地址</param>
            /// <param name="_username">管理员用户名</param>
            /// <param name="_password">密码</param>
            public Win32_UserAccount(string _ip, string _username, string _password)
            {
                IP = _ip;
                UserName = _username;
                PassWord = _password;
            } 

            /// <summary>
            /// 返回所有用户
            /// </summary>
            /// <returns>帐号数组</returns>
            public string[] ViweUsers()
            {
                try
                {
                    ConnectionOptions options = new ConnectionOptions();
                    options.Username = UserName;
                    options.Password = PassWord;

                    ManagementScope Conn = new ManagementScope(@"\\" + IP + @"\root\cimv2", options);
                    Conn.Connect();
                    //确定WMI操作的内容
                    ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_UserAccount");
                    ManagementObjectSearcher query1 = new ManagementObjectSearcher(Conn, oq);
                    //获取WMI操作内容
                    ManagementObjectCollection queryCollection1 = query1.Get();
                    //根据使用者选择,执行相应的远程操作
                    string[] s = new string[queryCollection1.Count];
                    int i=0;
                    foreach (ManagementObject mo in queryCollection1)
                    {
                        s[i] += mo.GetPropertyValue("Name");
                        i++;
                    }
                    return s;
                }
                //报错
                catch (Exception ee)
                {
                    throw ee;
                }
            }

            /// <summary>
            /// 修改帐号密码
            /// </summary>
            /// <param name="_ChangeUserName">要修改的帐号</param>
            /// <param name="_ChangePassWord">修改后的密码</param>
            /// <returns>是否成功</returns>
            public bool ChangePassWordByCmd(string _ChangeUserName,string _ChangePassWord)
            {
                try
                {

                    ConnectionOptions options = new ConnectionOptions();
                    options.Username = UserName;
                    options.Password = PassWord;

                    ManagementScope Conn = new ManagementScope(@"\\" + IP + @"\root\cimv2", options);
                    Conn.Connect();

                    ManagementPath path = new ManagementPath("Win32_Process");
                    ManagementClass processClass = new ManagementClass(Conn, path, null);

                    ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

                    //Parameters for creation of process.
                    inParams["CommandLine"] = "net user " + _ChangeUserName + " "+_ChangePassWord+"";

                    //Invoke Method.
                    ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);

                    return true;
                }
                //报错
                catch (Exception ee)
                {
                    return false;
                }
            }

            /// <summary>
            /// 添加用户
            /// </summary>
            /// <param name="_NewUserName">用户名</param>
            /// <param name="_NewPassWord">密码</param>
            /// <returns>是否成功</returns>
            public bool AddUserByCmd(string _NewUserName,string _NewPassWord)
            {
                try
                {

                    ConnectionOptions options = new ConnectionOptions();
                    options.Username = UserName;
                    options.Password = PassWord;

                    ManagementScope Conn = new ManagementScope(@"\\" + IP + @"\root\cimv2", options);
                    Conn.Connect();

                    ManagementPath path = new ManagementPath("Win32_Process");
                    ManagementClass processClass = new ManagementClass(Conn, path, null);

                    ManagementBaseObject inParams1 = processClass.GetMethodParameters("Create");

                    //Parameters for creation of process.
                    inParams1["CommandLine"] = "net user "+_NewUserName+" "+_NewPassWord+" /add";


                    //Invoke Method.
                    ManagementBaseObject outParams1 = processClass.InvokeMethod("Create", inParams1, null);
                    ManagementBaseObject inParams2 = processClass.GetMethodParameters("Create");
                    inParams2["CommandLine"] = "net localgroup administrators " + _NewUserName + " /add ";
                    ManagementBaseObject outParams2 = processClass.InvokeMethod("Create", inParams2, null);
                    return true;
                }
                //报错
                catch
                {
                    return false;
                }
            }

            /// <summary>
            /// 删除用户
            /// </summary>
            /// <param name="_DelUserName">要删除的用户名</param>
            /// <returns>是否成功</returns>
            public bool DelUserByCmd(string _DelUserName)
            {
                try
                {
                    ConnectionOptions options = new ConnectionOptions();
                    options.Username = UserName;
                    options.Password = PassWord;

                    ManagementScope Conn = new ManagementScope(@"\\" + IP + @"\root\cimv2", options);
                    Conn.Connect();

                    ManagementPath path = new ManagementPath("Win32_Process");
                    ManagementClass processClass = new ManagementClass(Conn, path, null);

                    ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

                    inParams["CommandLine"] = "net user " + _DelUserName + " /delete";

                    ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);

                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

  • 相关阅读:
    LeetCode 93. Restore IP Addresses
    LeetCode 92. Reverse Linked List II
    LeetCode 94. Binary Tree Inorder Traversal
    javaweb中重定向和请求转发(response.sendRedirect()和request.getRequestDispatcher(rul).forward(request,response)))的区别
    java关于jdbc的配置与使用步骤
    关于php中的include html文件的问题,为什么html可以在php中执行
    yii2 无法显示debug条的问题解决方法
    elasticsearch报错expected <block end>, but found BlockMappingStart解决方法
    sysctl -p 报错问题的解决方法
    yii2 Rbac使用yii命令一键建表
  • 原文地址:https://www.cnblogs.com/yinhaosln/p/1105625.html
Copyright © 2011-2022 走看看