给你一断用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;
}
}
}
|