以下.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 == 0) { _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 == 2224) { _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 11; } } } } }