Joining a Computer to the Domain with WScript/VBS (paramaters) and logging

Joining a Computer to the Domain with WScript (paramaters)

Paramater 1 = Domain (FQDN)

Paramater 2 = User

Paramater 3 = Password

Paramater 4 = OU / Organization Unit

On Error Resume Next

' Get a Temp Folder for Log file
Set objShell = CreateObject("Wscript.Shell")
	TempDir = objShell.ExpandEnvironmentStrings("%temp%")
Set objShell = nothing

' Set these variables
strDomain = Wscript.Arguments(0) ' Domain to logon from first paramater
strUser = Wscript.Arguments(1) ' Service account from second paramater
strPassword = Wscript.Arguments(2)AD ' Service account logon password from third paramater 
strOU = Wscript.Arguments(3) OU to place computer in from fourth paramater 

strLogFile = "result.txt"
strLofFilePath = TempDir

' Constants to choose from when joining
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

' Parameters check
if WScript.Arguments.Count < 4 then
	WScript.Echo  "Missing parameters " & cstr(WScript.Arguments.Count)
	WScript.quit
end if

Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName

' Join Domain
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\" & _
strComputer & "rootcimv2:Win32_ComputerSystem.Name='" & _
strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
strPassword, strDomain & "" & strUser, strOU, JOIN_DOMAIN + ACCT_CREATE + DOMAIN_JOIN_IF_JOINED)

Select Case ReturnValue
	Case 0 Status = "Success"
	Case 2 Status = "Missing OU"
	Case 5 Status = "Access denied"
	Case 53 Status = "Network path not found"
	Case 87 Status = "Parameter incorrect"
	Case 1326 Status = "Logon failure, user or pass"
	Case 1355 Status = "Domain can not be contacted"
	Case 1909 Status = "User account locked out"
	Case 2224 Status = "Computer Account allready exists"
	Case 2691 Status = "Allready joined"
	Case Else Status = "UNKNOWN ERROR " & ReturnValue
End Select

' Writing into log file
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (strLofFilePath & strLogFile , ForAppending, True)
Set colServices =  GetObject("winmgmts:").ExecQuery ("Select * from Win32_Service")

objTextFile.WriteLine(date & " " & time & vbTab & "Join a Computer**" & vbTab & Status & "**" & vbTab & "into '" & strDomain & "' domain. It has been tested with these credentials.** " & strUser & " " & strPassword  & " OU =**" & strOU)

objTextFile.Close

' Reboot computer if AD Join is success
if Status = "Success" then
	Dim objShell
	Set objShell = WScript.CreateObject("WScript.Shell")
	objShell.Run "C:WINDOWSsystem32shutdown.exe -r -t 100 "
Else
	WScript.Echo "Error occured becuase of " & Status
End if

WScript.quit
  1. Joining a Computer to the Domain with WScript/VBS (paramaters) and logging « Cem Güneyli

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: