使用System.DirectoryServices来操作组,用户的添加和删除

 编写自动化代码的朋友经常会遇到添加,删除组,用户之类的操作,我们通过命令行的net localgroup /add 也可以实现,但是对钟爱.net的人来说,我们也有办法。看如下代码:

 

using System.DirectoryServices;
using System;
using System.Collections.Generic;

namespace ToolsKit
{
 public class ADDGroup
 {
    static void Main(string[] args)
    {

       //通过命令行输入需要添加的组的名字和描述, 注意我这里使用空格来间隔
       List<GroupInfo> groups=CreateList(args);

       //初始化DirectoryEntry对象
       DirectoryEntry root = new DirectoryEntry("WinNT://" +Environment.MachineName + ",computer");
    try
    {
              //AddGroupByList(ref root,groups);  
              //DeleteGroup(root,"AeTLAS Administration");
              //AddUserToGroup(root,"Guests");
              DeleteUserFromGroup(root,"Guests","qigaopan");
          }
          catch(Exception ex)
          {
              Console.WriteLine(ex);
          }
          root.Close();
    }
    /*ADD GROUPS*/
    static void AddGroupByList(ref DirectoryEntry root,List<GroupInfo> list)
    {
       DirectoryEntry groups;
       foreach(GroupInfo info in list)
       {
           groups= root.Children.Add(info.GroupName,"group");
           groups.Properties["description"].Add(info.Description);
           groups.CommitChanges();  
       }  
    }
    /*Delete GROUPS*/
    static void DeleteGroup(DirectoryEntry root,string Name)
    {
          DirectoryEntry group = root.Children.Find(Name);
          if (group != null)
              root.Children.Remove(group);
    }
    /*Modify a Group*/
    static void ModifyGroup(DirectoryEntry root,string Name)
    {
           DirectoryEntry group = root.Children.Find(Name);
           if (group != null)
           {
               group.Properties["description"].Value = "Change the description.";
               group.Rename("NewName");
               group.CommitChanges();
           }
           root.Close();
    }
    //ADD A USER
    static void AddUser(DirectoryEntry root)
    {
           DirectoryEntry newUser = root.Children.Add("ab", "user");
           newUser.Invoke("SetPassword", new object[] { "Wangwenyan1983!12^"});
           newUser.Properties["description"].Add("This is a tested user");
           newUser.CommitChanges();
    }
   
    /*Add a user to specified group*/
    static void AddUserToGroup(DirectoryEntry root,string groupname)
    {
        DirectoryEntry newUser = root.Children.Add("qigaopan", "user");
           newUser.Invoke("SetPassword", new object[] { "Wangwenyan1983!12^"});
           newUser.Properties["description"].Add("This is a tested user");
           newUser.CommitChanges();
              
           DirectoryEntry grp=root.Children.Find(groupname,"group");
           Console.WriteLine(grp.Name);
           Console.WriteLine(newUser.ToString());
           if(grp.Name!="")
           {
              grp.Invoke("Add",new Object[]{newUser.Path.ToString()});
           }
          
        Console.WriteLine("Account Created Successfully");
        Console.ReadLine();
    }
    /*Delete a user from a specified group*/
    static void DeleteUserFromGroup(DirectoryEntry root,string groupname,string username)
    {
        DirectoryEntry grp=root.Children.Find(groupname,"group");
        Console.WriteLine(grp.Name);
        DirectoryEntry newUser=root.Children.Find(username,"user");
        Console.WriteLine(newUser.Name);
        if (grp != null) {grp.Invoke("Remove", new object[] {newUser.Path.ToString()});}
    }
    static List<GroupInfo> CreateList(string[] source)
    {
       int len=source.Length;
       List<GroupInfo> list=new List<GroupInfo>();
       GroupInfo group;
       for(int i=0;i<len;i++)
       {
          group=new GroupInfo(source[i],source[++i]);
          list.Add(group);
       }
       return list;
    }
 }
 class GroupInfo
 {
     string groupName;
     string description;
     public GroupInfo(string name,string descriptions)
     {
        this.groupName=name;
        this.description=descriptions;
     }
     public string GroupName
     {
        get
        {
           return this.groupName;
        }
     }
     public string Description
     {
        get
        {
           return this.description;
        }
     }
 }
}

 

注释不是很多,但是因为代码比较简单,所以代码本身就是注释。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值