以编程方式将本地用户添加到本地组

时间:2010-03-06 02:36:26

标签: c# active-directory wmi directoryservices active-directory-group

我正在开发针对WinXP,Vista和7个操作系统的C#应用​​程序。

一个功能是,我可以通过编程方式添加,删除,修改组设置为用户。

我可以请求帮助如何实现这一目标吗?

是否可以在WMI中执行此操作?我的代码主要使用WMI来获取用户..


目前正在使用Windows7

我正在尝试测试此代码

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer");
localMachine.Properties["member"].Add("Chevi");
localMachine.CommitChanges();
localMachine.Close();

并且它正在吐出这个错误

  

在缓存中找不到目录属性。

我尝试枚举Property集合,我得到了这个

OperatingSystem

OperatingSystemVersion

Owner

Division

ProcessorCount

Processor

Name

3 个答案:

答案 0 :(得分:2)

如果您正在使用本地组,则可以通过调用系统net命令来执行此操作。例如,要将用户添加到组,您可以调用:

net localgroup MyGroup /add SomeUser

在命令提示符下键入net help localgroup以获取更多信息。

您也可以使用WMI执行此操作。这是VBScript,但可以适应.NET或您首选的编程工具包:

Dim oComputer
Computer = "computername"
Groupname = "Administrators"
ObjectToAdd = "Administrator"

' Bind to the computer.
Set oComputer = GetObject("WinNT://" & Computer & ",computer")

' get group object
Dim oGroup
Set oGroup = oComputer.GetObject("group", GroupName)

' Add the user object to the group.
oGroup.Add "WinNT://" & Computer & "/" & ObjectToAdd 

信用:Matt Hickman,http://www.tech-archive.net/Archive/WinXP/microsoft.public.windowsxp.wmi/2004-04/0007.html

答案 1 :(得分:1)

等等

答案 2 :(得分:1)

我还使用C#在Visual Studio 2010上开发了一个Windows应用程序。这是该程序的工作版本,它将现有用户添加到特定组。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace Utility_Add_User_To_Group {

    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }

        private void btn_Add_Click(object sender, EventArgs e) {
            string usr, grp;
            usr = txt_UserName.Text;
            grp = txt_GroupName.Text;

            add(usr, grp);
            groupBox2.Visible=true;
        }

        private void add(string usr, string grp) {
            bool flagUsr, flagGrp;
            try {
                DirectoryEntry AD = new DirectoryEntry("WinNT://" +Environment.MachineName + ",computer");
                DirectoryEntry group, user;

                group = AD.Children.Find(grp, "group");
                user = AD.Children.Find(usr, "user");
                if (user != null) {
                    label3.Text += "User Name Exists!!!";
                    flagUsr = true;
                } else {
                    label3.Text += "Sorry, No Such User Name Found!!!";
                    flagUsr = false;
                }

                if (group != null) {
                    label4.Text += "Group Exists!!!";
                    flagGrp = true; 
                } else {
                    label4.Text += "Sorry, Group Does Not Exists!!!";
                    flagGrp= false;
                }

                if(flagGrp == true && flagUsr == true) {
                    group.Invoke("Add", new object[] { user.Path.ToString() });
                    label5.Text += "Congratulations!!! User has been added to the group";
                } else {
                    label5.Text += "Error Happened!!! User could not be added to the group!!!";
                }
            } catch (Exception e) {
                label6.Text +=e.Message.ToString();
                label6.Visible= true;
            }
            }

        private void btn_Clear_Click(object sender, EventArgs e) {
            normal();
        }
        private void normal() {
            txt_GroupName.Text="";
            txt_UserName.Text="";
            txt_UserName.Focus();

            groupBox2.Visible=false;
        }
        }
    }