Try the below script to join a computer to a network remotely:
Code:
Option Explicit
Dim objNetwork, strDomainName, strComputer
Dim strRemoteAdminUser, strRemoteAdminPass, strAdminUser, strAdminPass
Dim objShell, strCommand
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
strDomainName = objNetwork.UserDomain
strComputer = InputBox("Enter the Computer Name or IP Address:", "Computer")
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")
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")
If Ping(strComputer) = 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 = "cmd /k 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)
objShell.Run strCommand, 1, True
Else
MsgBox strComputer & " could not be pinged."
End If
MsgBox "Done"
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
Bookmarks