[C#]创建Windows用户及组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.DirectoryServices.AccountManagement;
using System.Collections; namespace UserTrans
{
public static class Commons
{
static Configuration config;
static AppSettingsSection appSetting;
public static ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static bool CreateLocalWindowsAccount(string userName, string passWord, string displayName, string description, string groupName, bool canChangePwd, bool pwdExpires)
{
bool retIsSuccess = false;
try
{
PrincipalContext context = new PrincipalContext(ContextType.Machine);
UserPrincipal user = new UserPrincipal(context);
user.SetPassword(passWord);
user.DisplayName = displayName;
user.Name = userName;
user.Description = description;
user.UserCannotChangePassword = canChangePwd;
user.PasswordNeverExpires = pwdExpires;
user.Save(); GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName);
group.Members.Add(user);
group.Save();
retIsSuccess = true;
}
catch (Exception ex)
{
retIsSuccess = false;
}
return retIsSuccess;
} static GroupPrincipal CreateGroup(string groupName, Boolean isSecurityGroup)
{
GroupPrincipal retGroup = null;
try
{
retGroup = IsGroupExist(groupName);
if (retGroup == null)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
GroupPrincipal insGroupPrincipal = new GroupPrincipal(ctx);
insGroupPrincipal.Name = groupName;
insGroupPrincipal.IsSecurityGroup = isSecurityGroup;
insGroupPrincipal.GroupScope = GroupScope.Local;
insGroupPrincipal.Save();
retGroup = insGroupPrincipal;
}
}
catch (Exception ex)
{ } return retGroup;
} static GroupPrincipal IsGroupExist(string groupName)
{
GroupPrincipal retGroup = null;
try
{
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
foreach (GroupPrincipal ingrp in srch.FindAll())
{
if (ingrp != null && ingrp.Name.Equals(groupName))
{
retGroup = ingrp;
break;
}
}
}
catch (Exception ex)
{ }
return retGroup;
}
public static int UpdateGroupUsers(string groupName, List<string> usersName)
{
List<string> addedUsers = new List<string>();
int retAddCount = 0; GroupPrincipal qbeGroup = CreateGroup(groupName, false);
foreach (UserPrincipal user in qbeGroup.GetMembers())
{
if (usersName.Contains(user.Name))
{
addedUsers.Add(user.Name);
retAddCount++;
}
else
{
user.Delete();
}
}
foreach (string addedUserName in addedUsers)
{
usersName.Remove(addedUserName);
}
foreach (string addUserName in usersName)
{
bool isSuccess = CreateLocalWindowsAccount(addUserName, "password", addUserName, "", groupName, false, false);
if (isSuccess) retAddCount++;
}return retAddCount;
}
}
}
参考
http://msdn.microsoft.com/zh-cn/library/bb924542(v=vs.90).aspx
[C#]创建Windows用户及组的更多相关文章
- Linux(CentOS6.5)下创建新用户和组,并制定用户和组ID
相关命令: groupadd -g 888 comexgroup useradd comex -d /comexHome -g comexgroup -u 888 cp /etc/skel/.* /c ...
- 管理员技术(四): 配置NTP网络时间客户端、 创建一个备份包、 配置用户和组账号、配置一个cron任务
一. 配置NTP网络时间客户端 目标: 本例要求配置虚拟机 server0,能够自动校对系统时间.相关信息如下: 1> NTP服务器位于 classroom.example.com ...
- 使用NetUserAdd API函数创建远程用户
http://apps.hi.baidu.com/share/detail/33407620 使用NetUserAdd编程创建远程用户Windows API NetUserAdd()可以创建Windo ...
- TFS 用户与组管理(转)
作者:frank.liu kaka.zhou 安装 Team Foundation Server 后,会创建以下全局组.可以使用这些全局组来控制 Team Foundation 用户的权限. 组 权限 ...
- TFS创建登录用户并连接TFS
简单使用 1.创建TFS组(Windows的组) 2.在“TFS管理控制台”中设置“组成员资格” 3.给“Team Foundation Administrators”组设置“属性” 4.给“Team ...
- C# 操作本地用户和组(基本全功能)
今天学习了下怎么用.Net操作本地用户和组,因为目前网上还没看到一篇比较完整的文章,所以整理了下也分享出来,最后附带参考文档,方便深究的童鞋继续学习.========== 原创作品 作者:Yo ...
- 009-centos6.6 用户以及组操作
1.查看用户所属组 id admin uid=500(admin) gid=500(admin) 组=500(admin) 2.查看用户信息 用户列表文件:cat /etc/passwd 查看系统中有 ...
- 管理用户和组 、 tar备份与恢复 、 cron计划任务-云计算学习(4)
配置用户和组账号 问题 本例要求创建下列用户.组以及组的成员关系: 新建用户 alex,其用户ID为3456,密码是flectrag 创建一个名为 adminuser 的组 创建一个名为 natash ...
- SharePoint 2013 SqlException (0x80131904):找不到Windows NT 用户或组xxxx\administrator
过程描述: 在SharePoint 2013里配置创建搜索服务应用程序时报错: 配置 Search Service 应用程序期间遇到错误. System.Data.SqlClient.SqlExcep ...
随机推荐
- Run-time Settings 运行时设置
1.执行顺序设置和执行概率设置 2.迭代之间的等待时间设置 3.日志设置 4.思考时间 5.杂项 线程进程选择: 一般服务器没有安全机制选择线程执行 但是有安全机制的话 比如第一个进程50个线程 第二 ...
- Java 经典练习题_Day010
final 变量能被显式地初始化并且只能初始化一次.被声明为 final 的对象的引用不能指向不同的对象.但是 final 对象里的数据可以被改变.也就是说 final 对象的引用不能改变,但是里面的 ...
- QT win 安装配置
QT windows 版安装配置 安装包:链接:https://pan.baidu.com/s/1LCj2V3xQ1wB9_7zmE5tV6Q 密码:bn9r 首先安装QT Creator 双击安装文 ...
- c#中的dynamic类型
dynamic是C#4.0引入的全新类型,它允许其操作略过编译期类型检查,而在运行时期处理. dynamic类型在大多数情况下和object类似,不同点在于编译器对于dynamic类型的 数据不做进一 ...
- 常用的ubantu操作命令
Ubuntu软件操作的相关命令 sudo apt-get update 更新源 sudo apt-get install package 安装包 sudo apt-get remove package ...
- 学python着几个要搞清楚WSGI和uWSGI区别
1 WSGI是一种通信协议 2 uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信. 3 而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器.
- 使用python读取大文件
python中读取数据的时候有几种方法,无非是read,readline,readlings和xreadlines几种方法,在几种方法中,read和xreadlines可以作为迭代器使用,从而在读取大 ...
- Spring启动时获取自定义注解的属性值
1.自定义注解 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- Cascade属性的取值
Cascade属性的取值有:1.none:忽略其他关联的对象,默认值.2.save-update:当session通过save(),update(),saveOrUpdate()方法来保存或更新对象时 ...
- 解决docker tty窗口太小,命令换行的问题
docker exec -it -e LINES=$(tput lines) -e COLUMNS=$(tput cols) ed08 bash