Monday, January 26, 2009

Join domain using script

The following vb script join a client machine in to a domain.
1. Assume client machine name is "dnsclient, domain is "myrootdns2003.com", domain user with admin preveleges is administrator@myrootdns2003.com,
domain user password is "Mydns123"

2. Then run the below script (joindomain.wsf) on command prompt as

joindomain.wsf dnsclient /NWname:"myrootdns2003.com" /user:"Administrator" /Password:"Mydns123" /NWType:"1"


'###################################################
' Join Domain - joindomain.wsf
'###################################################


Password = ""
UserName = ""
DomainWG = ""
NWType = "1"


Call SetLogLevel()
Call SetVars()


Function ExecuteWMICommand(WMIEchoStr)
Set objWMIService = GetObject("winmgmts:\root\cimv2")
Set SystemInstances = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each Instance in SystemInstances
TraceLog "Credentials: " & DomainWG & "|" & Password & "|" & UserName & "|" & "|" &NWType
ExecuteWMICommand = Instance.joinDomainOrWorkgroup(DomainWG,Password,UserName,NULL,NWType)


Next
WMIEchoStr = DomainWG
End Function

Function ExecuteValidationCommand(ValidationEchoStr)
If ( NWType = "1" ) then
'Join to domain
OutStr = ExecCommand( "net config workstation | Findstr Domain")
Else
'Join to Workgroup
OutStr = ExecCommand( "net config workstation | Findstr Workstation.domain")
End If

ValidationEchoStr = StringGetLastWord (OutStr)
ExecuteValidationCommand = "0"
End Function

Function CompareResults(WMIReturn, WMIEchoStr, ValidationReturn, ValidationEchoStr, OperationResultStr )
OperationResultStr = "Machine join failed"
CompareResults = OperationFailure
If StrComp(WMIReturn,ValidationReturn,1) = 0 Then
if StrComp(ValidationEchoStr,WMIEchoStr,1) = 0 Then
CompareResults = OperationSucess
OperationResultStr = "Machine joined successfully"
End If

End If
End Function

Sub SetVars ()
Set colNamedArguments = WScript.Arguments.Named
UserName = colNamedArguments.Item("User")
Password = colNamedArguments.Item("Password")
DomainWG = colNamedArguments.Item("NWName")
NWType = colNamedArguments.Item("NWType")
if ( StrComp ( NWType,"0",1 ) = 0 ) then
NWType = 0
Else
NWType = "1"
End if

End Sub





0 comments:

Design by infinityskins.blogspot