首页 技术 正文
技术 2022年11月6日
0 收藏 800 点赞 935 浏览 2821 个字

以下.net代码中共有两个方法。

AddComputerToDomain实现给把本计算机添加到某个域中

AddDomainUserToLocalAdminGroup实现把域中某用户添加到本地管理员

请添加引用System.DirectoryServices,在.NET下

具体代码如下:

using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using log4net;namespace ZhengShuangliang
{
class Program
{
static ILog _log=LogManager.GetLogger(typeof(Program)); static void Main(string[] args)
{
string domain = "domainname.com";
string account = "hellodev";
string password = "P@swrd1"; AddComputerToDomain(domain ,account,password); //string addedusername = "zslservices";
//string localgroup = "Administrators";
//AddDomainUserToLocalAdminGroup(domain, addedusername, localgroup); Console.Read();
} public static void AddDomainUserToLocalAdminGroup(string domainname, string username, string localgroup)
{
try
{
string userPath = string.Format("WinNT://{0}/{1},user", domainname, username);
string groupPath = string.Format("WinNT://{0}/{1},group", Environment.MachineName, localgroup);
using (DirectoryEntry group = new DirectoryEntry(groupPath))
{
group.Invoke("Add", userPath);
group.CommitChanges();
}
Console.WriteLine(string.Format(@"Add domain user: {0}\{1} to local group: {2} successed!", domainname,
username, localgroup));
_log.Debug(string.Format(@"Add domain user: {0}\{1} to local group: {2} successed!", domainname,
username, localgroup));
}
catch (System.DirectoryServices.DirectoryServicesCOMException ex)
{
_log.Error("AddToGroup occur Error:", ex);
}
} static void AddComputerToDomain(string domain ,string account,string password)
{
uint result = Join.DomainJoin("", domain, "", account, password);
if (result == )
{
_log.Debug(string.Format("Add to domain: {0} successed!", domain));
Console.WriteLine(string.Format("Add to domain: {0} successed!", domain));
}
else
{
_log.Debug(string.Format("Add to domain: {0} failed!, errorCode:{1}", domain, result));
Console.WriteLine(string.Format("Add to domain: {0} failed!, errorCode:{1}", domain, result));
} Console.Read();
} public class Join
{ [DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
static extern uint NetJoinDomain(
string lpServer,
string lpDomain,
string lpAccountOU,
string lpAccount,
string lpPassword,
JoinOptions NameType); [Flags]
enum JoinOptions
{
NETSETUP_JOIN_DOMAIN = 0x00000001,
NETSETUP_ACCT_CREATE = 0x00000002,
NETSETUP_ACCT_DELETE = 0x00000004,
NETSETUP_WIN9X_UPGRADE = 0x00000010,
NETSETUP_DOMAIN_JOIN_IF_JOINED = 0x00000020,
NETSETUP_JOIN_UNSECURE = 0x00000040,
NETSETUP_MACHINE_PWD_PASSED = 0x00000080,
NETSETUP_DEFER_SPN_SET = 0x10000000
} public static uint DomainJoin(string server, string domain, string OU, string account, string password)
{
try
{
uint value1 = NetJoinDomain(server, domain, OU, account, password, (JoinOptions.NETSETUP_JOIN_DOMAIN | JoinOptions.NETSETUP_DOMAIN_JOIN_IF_JOINED | JoinOptions.NETSETUP_ACCT_CREATE));
if (value1 == )
{
_log.Debug("Go through to 2224, Existing computer account found....");
value1 = NetJoinDomain(null, domain, null, account, password, (JoinOptions.NETSETUP_JOIN_DOMAIN));
}
return value1;
}
catch (Exception e)
{
_log.Error(e);
Console.WriteLine(e.Message);
return ;
}
}
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902