Re: VBScript to join domain
Moved From The FAQ Section
Welcome to the forms. :wave:
A bad user name or password will result in a specific error with a specific error number which you can trap for. This will never occur in your code, however, because you are using "On Error Resume Next"
You don't want to do that. You want the error to happen so you can deal with it.
Re: VBScript to join domain
You can skip the drive mapping instead, use the WBEM locater & connect server objects to connect with alternate credentials and set remote boot privilege. I didn’t test this but I think its’ good. - Scott
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
Const WBEM_IMPERSONATE = 3
Const wbemPrivilegeRemoteShutdown = 23 ' Required to shut down a system using a network request.
Const wbemPrivilegeShutdown = 18 ' Required to shut down a local system
Set objNetwork = CreateObject("WScript.Network")
strComputer = "RemoteServerName"
strLocalUser = "LocalServerUserName"
strLocalUserPassword = "LocalServerPassword"
strNetAdminUser = "NetworkUserAdmin"
strNetAdminPassword = "NetworkAdminPassword"
On Error Resume Next
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemService = objSWbemLocator.ConnectServer(strComputer , "root\cimv2", strUser, strPassword)
objSWbemService.Security_.ImpersonationLevel=WBEM_IMPERSONATE
If Err <> 0 Then
'Do something here
End if
On Error GoTo 0
On Error Resume next
Set objComputer = objSWbemService.get("\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain,strNetAdminPassword, strDomain & "\" & strNetAdminUser, NULL, JOIN_DOMAIN + ACCT_CREATE)
If Err <> 0 Then
'Do something here
End if
On Error GoTo 0
On Error Resume next
objSWbemService.Security_.Privileges=wbemPrivilegeRemoteShutdown
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Reboot()
Next
If Err <> 0 Then
'Do something here
End if
On Error GoTo 0