1. 编写BAT文件:

@echo off
cd /
WSCRIPT //E:vbscript test.vbs
echo


2. 编写VBS脚本


Option Explicit

Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144

Dim objNetwork,strComputer,strDomain,strPassword,strAdminUser,strOU,ReturnValue,objComputer
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
 
strDomain = "域"
strPassword = "密码"
strAdminUser = "用户名"
strOU = "OU=计算机,OU=XXX,OU=XXX,DC=XXX,DC=XXX"
Set objComputer = GetObject("winmgmts:{authenticationLevel=pktPrivacy,impersonationLevel=Impersonate}!\\" & _
     strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
        strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
     strPassword, strDomain & "\" & strAdminUser,strOU, JOIN_DOMAIN + ACCT_CREATE)
         
    If ReturnValue = 0 Then
    CALL AddDomainUsertoLocalAdmins
   ElseIf ReturnValue = 5 Then
    MsgBox "Access Denied, Please check the account to see if this account has permission",True
   ElseIf ReturnValue = 2691 Then
    MsgBox  "This Machin is already joined to a domain",True
   ElseIf ReturnValue = 1355 Then
    msgbox "The Sepicifed can not be contacted, please check your network or DNS settings",True
   ElseIf ReturnValue = 1398 Then
    msgbox "The time of computer is not correct with the Domain Controller, please correct the time",True
   ElseIf ReturnValue = 1311 Then
    msgbox "Can not find the Domain Controller, please make sure if there is a Domain controller",True
   ElseIf ReturnValue = 8557 Then
    msgbox "The User has joined so many computers that he has exceeded tht default per user computer quota",True
   Else
    msgbox "Error - Failed to Join Domain, the Error code is " & ReturnValue,True
   End If
         

Function AddDomainUsertoLocalAdmins

Dim objAdmins,LocalComputerName,UserAccountPath,UserAccount,domainname,Exec

domainname="XXXX.COM.CN"

End Function

  
WScript.Echo "成功! 请手动重启计算机使用XXX域用户登录!"