Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Joining machine to AD in specific OU

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,33709
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Joining machine to AD in specific OU
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Joining machine to AD in specific OU - 4/20/2006 3:28:37 AM   
  awe3s

 

Posts: 21
Score: 0
Joined: 4/20/2006
Status: offline
I have the following script that joins a machine in AD in a specified OU. It calls a htm file located in a network share. User name and password is provided and then it puts machine into the domain. My question is: it runs but the machine doesn't go into the domain. Can someone please provide an input into where I'm going wrong? Thanks


Dim strUser, strPassword, strDomain
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
' Declare domain and the OU
strDomain   = "MYDOMAIN.DOMAIN.COM"
strOU     = "OU=Computers,OU=OUNAME,OU=OUNAME,DC=MYDOMAIN,DC=DOMAIN,DC=com"
' Disconnect drive Z if mapped and then re mapped it
Set objNetwork = CreateObject("WScript.Network")
On Error Resume Next
objNetwork.RemoveNetworkDrive "Z:"
On Error Goto 0
objNetwork.MapNetworkDrive "Z:", "\\SERVERNAME\SHARE", , "USERNAME", "PASSWORD"
' Call the HTML file to input username and password
On Error Resume Next
Set objExplorer = WScript.CreateObject _
   ("InternetExplorer.Application", "IE_")
objExplorer.Navigate "\\SERVERNAME\SHARE\password.htm"   
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 490
objExplorer.Left = 300
objExplorer.Top = 200
objExplorer.Visible = 1            
Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
   Wscript.Sleep 250                
Loop
strUser = objExplorer.Document.Body.All.UserName.value
strPassword = objExplorer.Document.Body.All.UserPassword.Value
strButton = objExplorer.Document.Body.All.OKClicked.Value
objExplorer.Quit
Wscript.Sleep 250
If strButton = "Cancelled" Then
   Wscript.Quit
Else
' Join machine to the domain on the specified OU
strComputer = objNetwork.ComputerName
Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
  strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
      strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
   strPassword, strDomain & "\" & strUser, strOU, _
   JOIN_DOMAIN + ACCT_CREATE)
END If
' Reboot machine
Set WshShell=WScript.CreateObject("WScript.Shell")
nREBOOT=wshShell.Run("shutdown -r -t 00",0,TRUE)
 
 
Post #: 1
 
 RE: Joining machine to AD in specific OU - 4/20/2006 3:59:58 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
Is your strOU correct?
according to MS:
Accent
[in, optional] Specifies the pointer to a constant null-terminated character string that contains the RFC 1779 format name of the organizational unit (OU) for the computer account. If you specify this parameter, the string must contain a full path, otherwise Accent must be NULL.
Example: "OU=testOU, DC=domain, DC=Domain, DC=com"

you could try changing strOU to NULL and see if that works.

-Z

(in reply to awe3s)
 
 
Post #: 2
 
 RE: Joining machine to AD in specific OU - 4/20/2006 7:24:35 AM   
  awe3s

 

Posts: 21
Score: 0
Joined: 4/20/2006
Status: offline
Thanks for your reply. Yes if I set to NULL it works fine, and also it worked for me once using the mentioned strOU, but what does my heading is that it doesn't work the other time. Also the Computer OU I want the machine to go into is under two other OUs, hence the line  "OU=Computers,OU=OUNAME,OU=OUNAME,DC=MYDOMAIN,DC=DOMAIN,DC=com" .

I was thinking that I made a mistake somewhere within the code, maybe someone else might shed a light on this.

(in reply to ziminski)
 
 
Post #: 3
 
 RE: Joining machine to AD in specific OU - 4/20/2006 9:26:51 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
Sorry I don't deal with AD enough to help further.

gl

-Z

(in reply to awe3s)
 
 
Post #: 4
 
 RE: Joining machine to AD in specific OU - 4/20/2006 7:13:20 PM   
  awe3s

 

Posts: 21
Score: 0
Joined: 4/20/2006
Status: offline
Thanks Ziminski. Could anyone else please help? Thanks

(in reply to ziminski)
 
 
Post #: 5
 
 RE: Joining machine to AD in specific OU - 5/9/2006 9:50:08 PM   
  awe3s

 

Posts: 21
Score: 0
Joined: 4/20/2006
Status: offline
I have found a solution for this . As I said before, it works sometime and sometimes fails. I found where the problem was. If the machine's account exists on the domain the line JOIN_DOMAIN + ACCT_CREATE was failing with error 2224 (meaning account exists). So I modified the script and included the following after the JOIN_DOMAIN + ACCT_CREATE

If ReturnValue = 0 Then      ' if machine has joined the domain and account created successfully
Wscript.Echo "Machine has joined the domain successfully. Please click OK to reboot"
End If
If ReturnValue = 2224 Then    ' if machine account already exists in the domain
Wscript.Echo "The computer account already exists." & VbCrlf & "Computer will now join the domain."
  ' only join the machine to the domain since account exists
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _            
   strPassword, strDomain & "\" & strUser, strOU, _
   JOIN_DOMAIN)
If Not ReturnValue = 0 Then    ' if the joining fails
Wscript.Echo "Joining machine to the domain has failed. Please try again"
Wscript.Echo "Error Number: " & Err.Number
Wscript.Echo "Error Description: " & Err.Description
Wscript.Quit
Else
Wscript.echo "Machine has joined the domain successfully. Please click OK to reboot"



(in reply to awe3s)
 
 
Post #: 6
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Joining machine to AD in specific OU Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts