Link to home
Start Free TrialLog in
Avatar of jskfan
jskfanFlag for Cyprus

asked on

VBScript to join computers to the domain.

I have a text file that has computer names in columns.
I need a VBscript that joins each computer to the domain and reboot it.

I need a vbscript not a NETDOM JOIN command, as I already have a question open for the same purpose but didn;t work out.

Thanks
Avatar of jskfan
jskfan
Flag of Cyprus image

ASKER

This script does it..
I wonder why it doesn't reboot the machine or prompts for reboot.
I also need someone to modify it so that it can read from a text file a computer name and join it to the domain then go to next computer name in the list
Avatar of jskfan

ASKER

Note:
This script is run from the computer that is to join join the domain.
the real goal is to make the script run from a different computer in the domain, Read the computer name from the text file, join it to the domain and reboot it, then move to the next computer in the list.

So the script I posted is just a first step for an export to make it elaborate and achieve the goals.
change this values as need

strDomain   = "domainname"
strPassword = "password"
strUser     = "administrator"

list and log file path and name here:

Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting)

added ping function to unreachable computers


strDomain   = "domainname"
strPassword = "password"
strUser     = "administrator"
 
Const ForReading = 1
Const ForWriting = 2
Const JOIN_DOMAIN             = 1
Const ACCT_CREATE             = 2
Const ACCT_DELETE             = 4
Const DOMAIN_JOIN_IF_JOINED   = 32
Const JOIN_UNSECURE           = 64
Const DEFERRED_SPN_SET        = 256
Const INSTALL_INVOCATION      = 262144
 
set wshshell = createobject("wscript.shell")
Set objNetwork = CreateObject("WScript.Network")  
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting)
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
If Reachable(strComputer) then
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)
 
wshshell.run "shutdown /r" 
Else
objlog.WriteLine strComputer & " Not Reachable !"
End If
Loop
 
Function Reachable(strComputer)
 
 strCmd = "ping -n 1 " & strComputer
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
 Reachable = True 
 Else
 Reachable = False
 End If
End Function

Open in new window

Avatar of jskfan

ASKER

I am getting this message:
Client Connection to WINMGMT needs to be encrypted for this operation. Please adjust your IWBemservices proxy security settings and retry.
it's referring to this line 28.1
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
oddd

try this one please
strDomain   = "domainname"
strPassword = "password"
strUser     = "administrator"
 
Const ForReading = 1
Const ForWriting = 2
Const JOIN_DOMAIN             = 1
Const ACCT_CREATE             = 2
Const ACCT_DELETE             = 4
Const DOMAIN_JOIN_IF_JOINED   = 32
Const JOIN_UNSECURE           = 64
Const DEFERRED_SPN_SET        = 256
Const INSTALL_INVOCATION      = 262144
 
set wshshell = createobject("wscript.shell")
Set objNetwork = CreateObject("WScript.Network")  
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting)
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
If Reachable(strComputer) Then
Set objComputer = GetObject("winmgmts:" & "{impersonationLevel=impersonate, authenticationLevel=pktPrivacy}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" _
    & strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
   strPassword, _
   strDomain & "\" & strUser, _
   NULL, _
   JOIN_DOMAIN+ACCT_CREATE)
 
if ReturnValue <> 0 then
    objlog.WriteLine "Join failed with error: " & ReturnValue
else
    objlog.WriteLine "Successfully Joined " & strComputer 
end if
 
wshshell.run "shutdown /r /m \\" & strComputer
Else
objlog.WriteLine strComputer & " Not Reachable !"
End If
Loop
 
Function Reachable(strComputer)
 strCmd = "ping -n 1 " & strComputer
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
 Reachable = True 
 Else
 Reachable = False
 End If
End Function

Open in new window

Avatar of jskfan

ASKER

I haven't tried your last script yet, but the one below worked fine.
I run it on a local computer it joined to the domain, but didn't reboot it. I rebooted it manually.

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 = "domainname"
strPassword = "!password"
strUser = "Administrator"
 
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
wscript.echo strcomputer
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)

Avatar of jskfan

ASKER


yehudaha:
I just got a chance to run your last script  you posted.
When I run it in the local computer, it works just fine, it joins the computer to the domain and reboots it. If I run it from my DC it gives me error:
Permissions Denied 'GetObject'
and it points to the following line of the script:
Set objComputer = GetObject("winmgmts:" & "{impersonationLevel=impersonate, authenticationLevel=pktPrivacy}!\\" & _
first of all you asked for a script that can read from a txt file

so why you run the script on a target computer reboot it and stop the script in the midddle ?

i don't know why the dc giving an error but it can be domain controller policy issue,
the fact is the script run perfect on another computers.
the best thing to do run the script on a computer that doesn't need to add to the domain,
point the script to the txt file path, make sure the txt file goes like this:

comp1
comp2
comp3
and so on ....

and thats it !
Avatar of jskfan

ASKER

I ran the script from the target computer just for test purposes, and it worked.
After then I disjoined the target computer from the domain, and run the script from my Domain Controller, and it gave me the error
Permissions Denied 'GetObject'
and it points to the following line of the script:
Set objComputer = GetObject("winmgmts:" & "{impersonationLevel=impersonate, authenticationLevel=pktPrivacy}!\\" & _
ASKER CERTIFIED SOLUTION
Avatar of yehudaha
yehudaha
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jskfan

ASKER

<<<any problem to run the script from another computer and not from the dc ?>>>
Wow....why it's not running from the DC,but from another computer member of the domain?
i don't have a clear answer to your question, but it can be a policy issue.
Avatar of jskfan

ASKER

Excellent work!!!!
Avatar of jskfan

ASKER

Excellent work!!!!
Thanks
Glad I Could Help :-)