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;
            }
        }
    }

posted on 2009-09-02 03:35  米高佐敦  阅读(670)  评论(0编辑  收藏  举报