• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 758
  • Last Modified:

Add Computer to AD w/ user input

I would like to input the computer name instead of just use the current name.
 
ERROR: "the remote server machine does not exist or is unavailable: 'getObject'"
 
Dim strComputerName
strComputerName = InputBox("Enter Computer Name click OK to continue","Computer Name")
 
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 = "FABRIKAM"
strPassword = "ls4k5ywA"
strUser = "shenalan"

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

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

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    strPassword, strDomain & "\" & strUser, NULL, _
        JOIN_DOMAIN + ACCT_CREATE)

NOTE: I do not want to use netdom.exe
0
visualcoat
Asked:
visualcoat
  • 2
1 Solution
 
rutten-dCommented:
if I understand your question correctly you want to retrieve the computername on which the script is running , correct?

the computername is a system variable that you can retrieve using two simple lines:

Dim shell
Set shell = WScript.CreateObject("WScript.Shell")
Set strComputer = shell.Environment("COMPUTERNAME")

so your script would be:

'start script

Dim strComputerName
'not needed anymore: strComputerName = InputBox("Enter Computer Name click OK to continue","Computer Name")

Dim Shell
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 = "FABRIKAM"
strPassword = "ls4k5ywA"
strUser = "shenalan"

Set shell = WScript.CreateObject("WScript.Shell")
Set strComputer = shell.Environment("COMPUTERNAME")
'not needed anymore : Set objNetwork = CreateObject("WScript.Network")
'not needed anymore : strComputer = strComputerName   'objNetwork.ComputerName

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

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    strPassword, strDomain & "\" & strUser, NULL, _
        JOIN_DOMAIN + ACCT_CREATE)
'end script


haven't tested it , hope this helps!

0
 
visualcoatAuthor Commented:
I think you miss understood.  I want to be able to prompt the user to enter a pc name and that become the pc name and then have it add to the AD.
0
 
rutten-dCommented:
the script needs the actual computer name in order to add it to the domain.
Where you get this computername , by user input or whatever query , doesn't matter -
the code that adds the computer to the domain must contain the current computer name.

If you want the script to give the user the ability to rename the computer and add it to the domain , you
will have to perform three steps-

run a script that renames the computer
reboot
run a script that adds the computer to the domain.

You can't simultaneously rename the computer and add it to a domain.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now