1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
|
Option Explicit
Dim objNetwork, strDomainName, strComputer
Dim strRemoteAdminUser, strRemoteAdminPass, strAdminUser, strAdminPass, boolPinged
Dim objShell, strCommand, intReturn
Dim objFSO, objInput, objLog, strInput, strLogFile
Const intForReading = 1
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
'strDomainName = objNetwork.UserDomain
strInput = InputBox("Enter the name of the file to read computer names from:", "Input File", "computers.txt")
strLogFile = InputBox("Enter the name of the file to record results to:", "Log File", "log.txt")
'strComputer = InputBox("Enter the Computer Name or IP Address:", "Computer", "YOURREMOTEPC")
strRemoteAdminUser = InputBox("Enter the local administrator account name for " & strComputer & ":", "Admin User Account on " & strComputer, "Administrator")
strRemoteAdminPass = InputBox("Enter the local password for " & strComputer & "\" & strRemoteAdminUser & ":", "Local Admin Password")
strDomainName = InputBox("Enter the domain name to add computers to:", "Domain Name", "domain.com")
strAdminUser = InputBox("Enter the domain account for " & strDomainName & " to join the computer to the domain with:", "Domain Admin User Account", "Administrator")
strAdminPass = InputBox("Enter the password for " & strDomainName & "\" & strAdminUser & ":", "Domain Admin User Password")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile(strLogFile, True)
objLog.WriteLine "Add Remote Computers To Domain: " & strDomainName & " - Script started " & Now
Set objInput = objFSO.OpenTextFile(strInput, intForReading, False)
While Not objInput.AtEndOfStream
strComputer = objInput.ReadLine
boolPinged = Ping(strComputer)
If boolPinged = True Then
' IF THE FIRST COMMAND FAILS, TRY USING THE SECOND COMMAND TO CONNECT TO THE REMOTE MACHINE WITH EXPLICIT CREDENTIALS
' Also, you can change the cmd /k to cmd /c and change objShell.Run strCommand, 1, True to objShell.Run strCommand, 0, True
' to hide the command prompt, but do not use cmd /k and 0, otherwise an open command prompt will stay hidden.
strCommand = "NETDOM JOIN " & strComputer & " /Domain:" & strDomainName & " /userD:" & strDomainName & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT"
'strCommand = "cmd /k NETDOM JOIN " & strComputer & " /Domain:" & strDomainName & " /userD:" & strDomainName & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT"
'strCommand = InputBox("Prompt", "Title", strCommand)
intReturn = objShell.Run(strCommand, 1, True)
If intReturn = 0 Then
objLog.WriteLine strComputer & " was joined to the domain, and is rebooting."
Else
objLog.WriteLine strComputer & " could not be added to the domain. Exit code " & intReturn
End If
Else
objLog.WriteLine strComputer & " could not be pinged."
End If
Wend
objInput.Close
objLog.WriteLine "Add Remote Computers To Domain: " & strDomainName & " - Script finished " & Now
objLog.Close
MsgBox "Done. Please see " & strLogFile
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shell")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
|