rename and join domain script

Discussion in 'Scripting' started by tdubb, Mar 7, 2008.

  1. tdubb

    tdubb Guest

    I cannot seem to join the domain with this script. Any idea why? It renames
    th computer but does not join


    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colComputers = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")

    strcomputer = InputBox("Enter New Computer Name")

    If strComputer = "" then
    Wscript.quit
    End If

    For Each objComputer in colComputers
    err = objComputer.Rename(strComputer)
    Next


    'Joining a computer to a domain
    '=========================
    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


    strDomain = InputBox("Enter Domain name to Join")
    strUser = InputBox("Enter Domain User to join domain")
    strPassword = Inputbox("Enter password")

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

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

    ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    strPassword, _
    strDomain & "\" & strUser, _
    "cn=Computers,DC=mydomain,dc=com", JOIN_DOMAIN + ACCT_CREATE)

    If ReturnValue = 0 Then
    MsgBox "Computer added to domain under old name without error.
    proceeding to change computer name. "
    Else
    MsgBox "Computer not added to domain successfully. Return value: " &
    ReturnValue
    End If
     
    tdubb, Mar 7, 2008
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.