找回密码
 加入
搜索
查看: 3471|回复: 4

[系统综合] 求Win7可用的加域脚本

[复制链接]
发表于 2012-3-6 16:05:59 | 显示全部楼层 |阅读模式
如题,求Win7可用的加域脚本,之前在论坛上搜索的几个版本的加域脚本在win7下不好使。
发表于 2012-3-6 17:17:34 | 显示全部楼层
这个我还没有试过
有时间了再找找
发表于 2012-3-13 09:20:25 | 显示全部楼层
给你一断用C#编的控件台应用程序吧
using System;
using System.Collections.Generic;
using System.Management;


namespace JoinDomain
{
    class Program
    {
        static int Main(string[] args)
        {
            string username;
            string password;
            string domain;
            int returnCode = 1;
            int i = 1;

            while (returnCode != 0 && i <= 5)
            {
                Console.WriteLine(@"************************************************************************ 
                                Join pc to AD 
  
 Adding this computer "  + Environment.MachineName + @" to AD. 
 1)Computer Must be connected  LAN 
 2)Current Computer name had been create in correct OU 
 3)Account with access right. 
 If 5 times failed, this step will be skipped. now is [" + i.ToString() + @"] times 
                
*************************************************************************");


                username = ReadUserName();
                
                Console.WriteLine(username);
                while (username.IndexOf(@"") == -1)
                {
                    Console.WriteLine(@"The Format is wrong!");
                    username = ReadUserName();
                }

                password = ReadPassword();
                while (password.Length < 6)
                {
                    Console.WriteLine();
                    Console.WriteLine(@"Password is too short, please try again!");
                    password = ReadPassword();
                }

                domain = ReadDomain(username);

                returnCode = JoinDomain(domain, password, username);

                if (returnCode == 0)
                {
                    Console.WriteLine("Join Domain Successfully");
                }
                else
                {
                    string strErrorMsg = "";
                    switch (returnCode)
                    {
                        case 5: strErrorMsg = "Access is denied";
                            break;
                        case 87: strErrorMsg = "The parameter is incorrect";
                            break;
                        case 110: strErrorMsg = "The system cannot open the specified object";
                            break;
                        case 1323: strErrorMsg = "Unable to update the password";
                            break;
                        case 1326: strErrorMsg = "Logon failure: unknown username or bad password";
                            break;
                        case 1355: strErrorMsg = "The specified domain either does not exist or could not be contacted";
                            break;
                        case 2224: strErrorMsg = "The account already exists";
                            break;
                        case 2691: strErrorMsg = "The machine is already joined to the domain";
                            break;
                        case 2692: strErrorMsg = "The machine is not currently joined to a domain";
                            break;
                        default:
                            strErrorMsg = returnCode.ToString() + "Unknow Reason!";
                            break;
                    }
                    Console.WriteLine("Failed:" + strErrorMsg + "\t Press any key to try again.");
                    Console.ReadKey(true);
                }

                i++;

            }
            return returnCode;

        }
        public static string ReadUserName()
        {
            Console.Write(@"->Plase Enter you id Format like (Domain\windowsID):");
            return Console.ReadLine().ToString();
        }
        public static string ReadDomain(string username)
        {
            string[] arrUserName = username.Split('\\');
            string strDomain;

            Console.WriteLine();
            Console.Write(@"-> Please Enter Domain Name( Press Enter will use " + arrUserName[0] + " as default):");
            strDomain = Console.ReadLine();
            if (strDomain.Trim().Length == 0)
            {
                return arrUserName[1].Trim().ToString();
            }
            else
            {
                return strDomain;
            }
        }

        public static int JoinDomain(string Domain, string password, string username)
        {
            const UInt32 JOIN_DOMAIN = 1;

            try
            {
                ManagementObject mo = new ManagementObject("Win32_ComputerSystem.Name='" + Environment.MachineName + "'");
                object[] methodArgs = { Domain, password, username, null, JOIN_DOMAIN };
                object oResult = mo.InvokeMethod("JoinDomainOrWorkgroup", methodArgs);
                return Convert.ToInt32(oResult);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 1;
            }
        }

        public static string ReadPassword()
        {
            Console.Write("->Please Enter you passcode, at least 6 characters:");
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    Console.Write("*");
                    password += info.KeyChar;
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        // remove one character from the list of password characters 
                        password = password.Substring(0, password.Length - 1);
                        // get the location of the cursor 
                        int pos = Console.CursorLeft;
                        // move the cursor to the left by one character 
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                        // replace it with space 
                        Console.Write(" ");
                        // move the cursor to the left by one character again 
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    }
                }
                info = Console.ReadKey(true);
            }
            // add a new line because user pressed enter at the end of their password 
            //Console.WriteLine(); 
            // Console.WriteLine(password); 
            return password;
        }
    }

}
发表于 2012-3-14 09:55:18 | 显示全部楼层
上面的可以用吗?有时间试试了。
发表于 2012-3-14 09:56:12 | 显示全部楼层
回复 3# wgboy


    我还以为是au3
原来是批处理呀?
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-20 17:34 , Processed in 0.074996 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表