Join a PC to the domain completely via VBScript with ADSI

From: EAGroff (eric.groff_at_memphisgroffs.com)
Date: 03/01/05


Date: 1 Mar 2005 14:46:21 -0800

I have been beating my head against the wall trying to get this script
working. I am trying to join a PC to the domain and have the machine
account in the proper OU based on machine name. I keep getting 80005000
errors when it gets tot he oRoot line. has anyone had any luck with
this?

'Script for joining a branch PC to the domain and placing it in the
proper OU
'
' For WinXP and above only!
' Author: Eric A. Groff
' Date: 02/23/2005 11:11

'On Error Resume Next

Dim oRoot, oOU, strContainer

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
Const ADS_SECURE_AUTHENTICATION = &H1
Const ADS_USE_ENCRYPTION = &H2
Const ADS_USE_SSL = &H2
Const ADS_READONLY_SERVER = &H4
Const ADS_PROMPT_CREDENTIALS = &H8
Const ADS_NO_AUTHENTICATION = &H10
Const ADS_FAST_BIND = &H20
Const ADS_USE_SIGNING = &H40
Const ADS_USE_SEALING = &H80

strDomain = GetResponse(" Domain Name", "Domain", "Domain")
strUser = GetResponse(" User ID", "administrator", "JQUser")
strPassword = GetResponse(" Password", "", "passwordhere")
strTMOU =
"LDAP://OU=WAN-Linked,OU=Computers,OU=Company,dc=domain,dc=com"
strTGOU = "Laptops"

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

Set objComputer =
GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
        strComputer & "'")

strClientType = Left(UCase(strComputer), 2)
Select Case strClientType
    Case "TG"
            Set oRoot = GetObject("LDAP:")
' Set oOU = oRoot.OpenDSObject(strTGOU, strDomain & "\" &
strUser, strPassword, ADS_SECURE_AUTHENTICATION) '
IADsOpenDSObject
        Set oOU = OpenDSObject(strTMOU, strDomain & "\" & strUser,
strPassword, ADS_SECURE_AUTHENTICATION) '
IADsOpenDSObject
        Set oComputer = oOU.Create("Computer" , "cn=" & strComputer)
        oComputer.Put "sAMAccountName", strComputer & "$"
        objComputer.SetInfo
            Set ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain,
strPassword, strDomain & "\" & strUser, NULL, JOIN_DOMAIN +
ACCT_CREATE)
            Set oOU = Nothing
            Set oComputer = Nothing
            Set ReturnValue = Nothing
            ' Set oOU = oRoot.OpenDSObject(strTGOU, strDomain & "\" &
strUser, strPassword, ADS_SECURE_AUTHENTICATION) '
IADsOpenDSObject
' ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain,
strPassword, strDomain & "\" & strUser, NULL, JOIN_DOMAIN +
ACCT_CREATE)
' Set objMoveComputer = objOU.MoveHere("LDAP://CN=" & strComputer &
",CN=Computers,dc=biz,dc=es,dc-svm,dc=com", "CN=" & strComputer)
    Case "TM"
            Set oRoot = GetObject("LDAP:")
        Set oOU = oRoot.OpenDSObject(strTMOU, strDomain & "\" &
strUser, strPassword, ADS_SECURE_AUTHENTICATION) '
IADsOpenDSObject
        Set oComputer = oOU.Create("Computer" , "cn=" & strComputer)
        oComputer.Put "sAMAccountName", strComputer & "$"
        oComputer.Setinfo
        MsgBox strComputer & " has been created in the " & strTMOU & "
container."
            Set ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain,
strPassword, strDomain & "\" & strUser, NULL, JOIN_DOMAIN +
ACCT_CREATE)
            MsgBox strComputer & " has been joined to the " & strDomain & "
Domain."
            Set oRoot = Nothing
            Set oOU = Nothing
            Set oComputer = Nothing
            Set ReturnValue = Nothing
End Select

Function GetResponse(sItem, sValue, sHint)
    GetResponse = InputBox ( "Enter your " & sItem & vbCrlf & _
                             "then Click Ok." & vbCrlf & vbCrlf & _
                             "Example: " & sHint, "Remote
Authentication", sValue)
End Function

I have tried to specify the domain LDAP ont he oRoot line but with no
success. It's like it refuses to bind to the OU for some reason. Any
ideas? I know I'm not the only one trying to do this...

EricG



Relevant Pages

  • Re: Setting environment variables in IIS 6.0 [PERL @INC MANIPULATION]
    ... included the sample drsaticly reduced VBS script below - the script will add ... > Only option I can think of is to write to the IIS Admin APIs using script. ... Const L_ChkCmdLibReg_ErrorMessage = "Please register the ...
    (microsoft.public.inetserver.iis)
  • Please help: Getting REGISTRY info w/ StdRegProv
    ... I'm building a script for software inventory purposes. ... Const REG_BINARY = 3 ... iValueName, sValue ...
    (microsoft.public.scripting.vbscript)
  • Script not Functioning Properly
    ... the script is supposed to automatically reply to any new ... Const CdoPR_ACTION_FLAG = &H10810003 ... Public Sub Folder_OnMessageCreated ...
    (microsoft.public.outlook.program_vba)
  • Re: From Workgroup to Domain
    ... Ok thank you for the help - nice to see the script run:) ... it should help the win2k clients. ... Gibraltar skrev: ... Const JOIN_DOMAIN = 1 ...
    (microsoft.public.windows.server.active_directory)
  • [code] getting your script to talk to you
    ... SAPI comes as an actX object, and so is programmable using vbs. ... There was only one small script for sapi that I could find here, ... Dim oSpVoice, sText ... Const SVSF_Default = 0 ...
    (microsoft.public.scripting.vbscript)