zoukankan      html  css  js  c++  java
  • .net程序实现给机器加域,添加域账号到本地管理员

    以下.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;
                    }
                }
            }
        }
    }
  • 相关阅读:
    hashCode()相同,equals()也一定为true吗?
    什么是装箱?什么是拆箱?装箱和拆箱的执行过程?常见问题?
    LOJ114_k 大异或和_线性基
    BZOJ_4459_[Jsoi2013]丢番图_数学+分解质因数
    BZOJ_4184_shallot_线段树按时间分治维护线性基
    BZOJ_2844_albus就是要第一个出场_线性基
    BZOJ_3105_[cqoi2013]新Nim游戏_线性基+博弈论
    BZOJ_1195_[HNOI2006]最短母串_AC自动机+BFS+分层图
    BZOJ_3881_[Coci2015]Divljak_AC自动机+dfs序+树状数组
    BZOJ_1532_[POI2005]Kos-Dicing_二分+网络流
  • 原文地址:https://www.cnblogs.com/zhengshuangliang/p/4235615.html
Copyright © 2011-2022 走看看