Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Script to add the logged in machine to the domain.

Hi,

Script to add the logged in machine to the domain.
Once we have installed the operating system i need a way to add the machine into the Domain with the details in the script.
I will later convert the vbs to an exe so they are secured,

1. Need to add the machine to the Domain
2. Every time run has to name the computer to a new machine name.
Like " Dev-chen-pc4001" then "Dev-chen-pc4002" and so on
My requirment for this is a lot need to do this with 600 + Systems before Monday...

Regards
Sharath
Avatar of bsharath
bsharath
Flag of India image

ASKER

I can provide the Domain name,Username and password within the script....

I will login as the local administrator to the machine.When run script has to rename the machine with the random names and then add the machine to the domain.
If the machine names automation is not possible or difficult can i have all the machine names listed in the script.So the script can ping for availability of the firdst in the row and assign if the name is not available in ADS.
Avatar of RobSampson
Sharath, here's a script that should be able to join a computer to the network remotely.  I should be able to modify it work directly from the non-domain computer.....

Regards,

Rob.
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

Open in new window

Thanks Rob,

Is the machine names to be provided some where?
Rob can all the hardcoded into the script instead of putting them manually.
Username,Password,Domain name

Rob can all the hardcoded into the script instead of putting them manually.
Username,Password,Domain name

Not yet....I'm working on that bit.....the above script probably isn't what you'd use anyway....but I'll combine a couple of scripts......

I'm just working on authenticating against the domain to perfom a computer lookup query from a non-domain PC.

Rob.
Ok Rob thanks....
OK, for the first step of finding the "next available" computer name, can you run this on a computer that is not on the domain and see if it finds the correct next available name?

Regards.

Rob.
' These are required, they are only implicit when pre-authenticated against the domain.
 
Const strDomainName = "yourdomainname"
Const strUsername = "username"
Const strPassword = "password"
Const strLDAP_Server = "maindc.domain.com"
Const strComputerPrefix = "DEV-CHEN-PC"
strStart = "4001"
 
Const ADS_SECURE_AUTHENTICATION = 1
Const ADS_USE_ENCRYPTION = 2
 
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADSDSOObject"
 
' Authenticate ADODB Connection
objConnection.Properties("User ID") = strDomainName & "\" & strUsername
objConnection.Properties("Password") = strPassword
 
objConnection.Open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 600
objCommand.Properties("Searchscope") = 2
objCommand.Properties("Cache Results") = False
 
' Create a simple LDAP Query for computer name
boolFound = False
While boolFound = False
	strQuery = "SELECT aDSPath FROM 'LDAP://" & strLDAP_Server & "' WHERE objectClass='Computer' AND Name='" & strComputerPrefix & strStart & "'"
	MsgBox strQuery
	objCommand.CommandText = strQuery
	Set objRecordSet = objCommand.Execute
	If Not objRecordSet.EOF Then
		strStart = CStr(CInt(strStart) + 1)
	Else
		boolFound = True
	End If
Wend
 
objConnection.Close
 
Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
 
MsgBox "Computer to be added: " & strComputerPrefix & strStart

Open in new window

Rob how will the script decide on the computer name?
Can we mention some names in the script.That it can take and ping each to see the next available name
Rob how will the script decide on the computer name?
Can we mention some names in the script.That it can take and ping each to see the next available name
No, it's not pinging machines.  That wouldn't  be very reliable because you may not have them all on, or the firewall might block the ping request.  The script will use the the domain user credentials to query the Active Directory for existing computer names based on these lines:

Const strComputerPrefix = "DEV-CHEN-PC"
strStart = "4001"

and it will continue to increment the strStart value.

Regards,

Rob.
Rob i get an error on line 34 char 2
Table does not exist
Rob i get an error on line 34 char 2
Table does not exist
You should get a query in a Message Box just before that.....does the query look formatted correctly?

Rob.
I get it right
ldap://Domainname.domain.co.uk
Does it searches the name just in the computers OU.?

I have names all over the place.I mean different ou's as well
I get it right
ldap://Domainname.domain.co.uk
Does it searches the name just in the computers OU.?

I have names all over the place.I mean different ou's as well
Hmmmm.....the
Domainname.domain.co.uk

can you ping that from a command line?

Once it gets past that, and connects, it will search the entire domain for the computer name, so it doesn't matter where the object are....we just need it to connect first....

Your message box should say something like
SELECT aDSPath FROM 'LDAP://dc.domain.com' WHERE objectClass='Computer' AND Name='dev-chen-pc4001'

Regards,

Rob.
Rob

Here what is it that i need to mention

Const strLDAP_Server = "maindc.domain.com"
Rob

Here what is it that i need to mention

Const strLDAP_Server = "maindc.domain.com"
yes rob i go this

SELECT aDSPath FROM 'LDAP://dc.domain.com' WHERE objectClass='Computer' AND Name='dev-chen-pc4001'

Now i get
Computer to be added Dev-chen-pc4001
when clicked enter nothing happens
yes rob i go this

SELECT aDSPath FROM 'LDAP://dc.domain.com' WHERE objectClass='Computer' AND Name='dev-chen-pc4001'

Now i get
Computer to be added Dev-chen-pc4001
when clicked enter nothing happens
Rob i think there must be some permission issue

In the
Const strdomainname = " i give the full domain name"
I get an 32 line permission denied error.

I have the right credentials in the file

The machine that i am adding is from a local administratopr account only.

I am able to ping with the name
Rob i think there must be some permission issue

In the
Const strdomainname = " i give the full domain name"
I get an 32 line permission denied error.

I have the right credentials in the file

The machine that i am adding is from a local administratopr account only.

I am able to ping with the name
I have tried this on a fresh machine that is installed. Logged in as the local administrator of the machine.
Plugged a USB drive that has this vbs file.Copied it to the D Drive and run the script...
Rob a reminder..... :-))
OK, that script was not going to join the computer to the domain just yet, but I've re-written it a bit, to include the renaming of the computer, then after a reboot, you run the script again, and it will hopefully prompt you to join the domain....

You should only need to change these lines:
      Const strDomainName = "yourdomain"
      Const strUsername = "username"
      Const strPassword = "password"
      Const strLDAP_Server = "maindc.domain.com"

Regards,

Rob.
Option Explicit
 
Dim objNetwork, objShell, objFSO, strRenameFlag, strComputerName, strDomain, strNextAvailableName
Dim objWMIService, colComputers, objComputer, intErrorCode, objOutputFile, strCommand
Dim strAdminUser, strAdminPass
 
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strRenameFlag = "C:\Temp\Renamed.txt"
 
strComputerName = objNetwork.ComputerName
 
If objFSO.FileExists(objFSO.GetSpecialFolder(1) & "\NetDom.exe") = False Then
	MsgBox "NetDom.exe does not exist at " & objFSO.GetSpecialFolder(1) & "\NetDom.exe.  Cannot continue script."
	WScript.Quit
End If
 
If objFSO.FolderExists("C:\Temp") = False Then
	objFSO.CreateFolder("C:\Temp")
End If
 
If objFSO.FileExists(strRenameFlag) = False Then
	strNextAvailableName = GetNextAvailablePCName
	If strNextAvailableName <> "" Then
		If UCase(strComputerName) <> UCase(strNextAvailableName) Then
			' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
			'strNextAvailableName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
			MsgBox "Computer will now be renamed to " & strNextAvailableName
			Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
			Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
			For Each objComputer in colComputers
				intErrorCode = objComputer.Rename(UCase(strNextAvailableName))
				If intErrorCode <> 0 Then
					WScript.Echo "Error renaming computer. Error # " & intErrorCode
				Else
					Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
					objOutputFile.Close
					Set objOutputFile = Nothing
					WScript.Echo "Computer renamed. It will now reboot. Please re-run this script again after logging in."
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					objShell.Run "shutdown -r -t 00", 1, False
				End If
			Next
		Else
			WScript.Echo "Computer already has the correct name."
			Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
			objOutputFile.Close
			Set objOutputFile = Nothing
			' Run the script again to have it joined to the domain.
			objShell.Run "wscript """ & WScript.ScriptFullName & """", 1, False
		End If
	Else
		WScript.Echo "Could not find the next available computer name."
	End If
Else
	' IF THE COMPUTER HAS BEEN RENAMED, THEN IT WILL JOIN THE DOMAIN
	'Alternate Join Computer code: http://msdn2.microsoft.com/en-us/library/aa392154.aspx
	objShell.Run "control timedate.cpl ,1", 1, False
	WScript.Echo "Please select the ""Time Zone"" tab and select your time zone, set your time, then click OK."
	strDomain = InputBox("Enter the domain that you want to join:", "Domain Name", "DefaultDomain")
	strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
	'strAdminUser = "Administrator"
	'strAdminPass = "*"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
	WScript.Echo strCommand
	'objShell.Run strCommand, 1, False
	
	' Now we need to stop the script from running again.
	'strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""wscript.exe " & WScript.ScriptFullName & """ /f"
	'objShell.Run strCommand, 0, True
	objFSO.DeleteFile strRenameFlag, True
	'objFSO.DeleteFile WScript.ScriptFullName, True
	
	WScript.Echo "Your computer will be joined to the domain shortly, and reboot automatically. Please wait."
End If
 
Function GetNextAvailablePCName
	' These are required, they are only implicit when pre-authenticated against the domain.
	 
	Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
	Const strDomainName = "yourdomain"
	Const strUsername = "username"
	Const strPassword = "password"
	Const strLDAP_Server = "maindc.domain.com"
	Const strComputerPrefix = "DEV-CHEN-PC"
	strStart = "4001"
	 
	Const ADS_SECURE_AUTHENTICATION = 1
	Const ADS_USE_ENCRYPTION = 2
	 
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADSDSOObject"
	 
	' Authenticate ADODB Connection
	objConnection.Properties("User ID") = strDomainName & "\" & strUsername
	objConnection.Properties("Password") = strPassword
	 
	objConnection.Open "Active Directory Provider"
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Timeout") = 600
	objCommand.Properties("Searchscope") = 2
	objCommand.Properties("Cache Results") = False
	 
	' Create a simple LDAP Query for computer name
	boolFound = False
	While boolFound = False
		strQuery = "SELECT aDSPath FROM 'LDAP://" & strLDAP_Server & "' WHERE objectClass='Computer' AND Name='" & strComputerPrefix & strStart & "'"
		objCommand.CommandText = strQuery
		On Error Resume Next
		Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			MsgBox "Error " & Err.Number & VbCrLf & "Description: " & Err.Description & VbCrLf & "Query: " & strQuery
			Err.Clear
			On Error GoTo 0
			strNewName = ""
			boolFound = True
		Else
			On Error GoTo 0
			If Not objRecordSet.EOF Then
				strStart = CStr(CInt(strStart) + 1)
			Else
				boolFound = True
				strNewName = strComputerPrefix & strStart
			End If
		End If
	Wend
	 
	objConnection.Close
	 
	Set objRecordSet = Nothing
	Set objCommand = Nothing
	Set objConnection = Nothing
	 
	GetNextAvailablePCName = strNewName
End Function

Open in new window

Hi Rob...

I get this message...
I checked the windows vista and windows 2003 machines cannot find this exe...
---------------------------

---------------------------
NetDom.exe does not exist at C:\WINDOWS\system32\NetDom.exe.  Cannot continue script.
---------------------------
OK  
---------------------------
Hi Rob...

I get this message...
I checked the windows vista and windows 2003 machines cannot find this exe...
---------------------------

---------------------------
NetDom.exe does not exist at C:\WINDOWS\system32\NetDom.exe.  Cannot continue script.
---------------------------
OK  
---------------------------
Rob i just downloaded the file and placed it in system32 and when run i get this message

---------------------------

---------------------------
Error -2147217911

Description: Permission denied.

Query: SELECT aDSPath FROM 'LDAP://inssrv01.development.group.co.uk' WHERE objectClass='Computer' AND Name='dev-chen-pc4001'
---------------------------
OK  
---------------------------
Rob i just downloaded the file and placed it in system32 and when run i get this message

---------------------------

---------------------------
Error -2147217911

Description: Permission denied.

Query: SELECT aDSPath FROM 'LDAP://inssrv01.development.group.co.uk' WHERE objectClass='Computer' AND Name='dev-chen-pc4001'
---------------------------
OK  
---------------------------
Hi, try this....I've added a new parameter to the query:
      objCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL

and also changed the script to copy NetDom.exe from the same folder as the script (so copy it to the USB drive along with the script) to the System32 folder.

Regards,

Rob.
Option Explicit
 
Dim objNetwork, objShell, objFSO, strRenameFlag, strComputerName, strDomain, strNextAvailableName
Dim objWMIService, colComputers, objComputer, intErrorCode, objOutputFile, strCommand
Dim strAdminUser, strAdminPass
 
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strRenameFlag = "C:\Temp\Renamed.txt"
 
strComputerName = objNetwork.ComputerName
 
If objFSO.FileExists(objFSO.GetSpecialFolder(1) & "\NetDom.exe") = False Then
	If objFSO.FileExists(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe") = True Then
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe", objFSO.GetSpecialFolder(1) & "\"
		MsgBox "NetDom.exe has been copied to " & objFSO.GetSpecialFolder(1) & "\"
	Else
		MsgBox "NetDom.exe does not exist at " & objFSO.GetSpecialFolder(1) & "\NetDom.exe.  Cannot continue script."
		WScript.Quit
	End If
End If
 
If objFSO.FolderExists("C:\Temp") = False Then
	objFSO.CreateFolder("C:\Temp")
End If
 
If objFSO.FileExists(strRenameFlag) = False Then
	strNextAvailableName = GetNextAvailablePCName
	If strNextAvailableName <> "" Then
		If UCase(strComputerName) <> UCase(strNextAvailableName) Then
			' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
			'strNextAvailableName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
			MsgBox "Computer will now be renamed to " & strNextAvailableName
			Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
			Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
			For Each objComputer in colComputers
				intErrorCode = objComputer.Rename(UCase(strNextAvailableName))
				If intErrorCode <> 0 Then
					WScript.Echo "Error renaming computer. Error # " & intErrorCode
				Else
					Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
					objOutputFile.Close
					Set objOutputFile = Nothing
					WScript.Echo "Computer renamed. It will now reboot. Please re-run this script again after logging in."
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					objShell.Run "shutdown -r -t 00", 1, False
				End If
			Next
		Else
			WScript.Echo "Computer already has the correct name."
			Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
			objOutputFile.Close
			Set objOutputFile = Nothing
			' Run the script again to have it joined to the domain.
			objShell.Run "wscript """ & WScript.ScriptFullName & """", 1, False
		End If
	Else
		WScript.Echo "Could not find the next available computer name."
	End If
Else
	' IF THE COMPUTER HAS BEEN RENAMED, THEN IT WILL JOIN THE DOMAIN
	'Alternate Join Computer code: http://msdn2.microsoft.com/en-us/library/aa392154.aspx
	objShell.Run "control timedate.cpl ,1", 1, False
	WScript.Echo "Please select the ""Time Zone"" tab and select your time zone, set your time, then click OK."
	strDomain = InputBox("Enter the domain that you want to join:", "Domain Name", "DefaultDomain")
	strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
	'strAdminUser = "Administrator"
	'strAdminPass = "*"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
	WScript.Echo strCommand
	'objShell.Run strCommand, 1, False
	
	' Now we need to stop the script from running again.
	'strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""wscript.exe " & WScript.ScriptFullName & """ /f"
	'objShell.Run strCommand, 0, True
	objFSO.DeleteFile strRenameFlag, True
	'objFSO.DeleteFile WScript.ScriptFullName, True
	
	WScript.Echo "Your computer will be joined to the domain shortly, and reboot automatically. Please wait."
End If
 
Function GetNextAvailablePCName
	' These are required, they are only implicit when pre-authenticated against the domain.
	 
	Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
	Const strDomainName = "yourdomain"
	Const strUsername = "username"
	Const strPassword = "password"
	Const strLDAP_Server = "maindc.domain.com"
	Const strComputerPrefix = "DEV-CHEN-PC"
	strStart = "4001"
	 
	Const ADS_SECURE_AUTHENTICATION = 1
	Const ADS_USE_ENCRYPTION = 2
	Const ADS_CHASE_REFERRALS_EXTERNAL = &H40
	 
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADSDSOObject"
	 
	' Authenticate ADODB Connection
	objConnection.Properties("User ID") = strDomainName & "\" & strUsername
	objConnection.Properties("Password") = strPassword
	 
	objConnection.Open "Active Directory Provider"
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Timeout") = 600
	objCommand.Properties("Searchscope") = 2
	objCommand.Properties("Cache Results") = False
	objCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL
	 
	' Create a simple LDAP Query for computer name
	boolFound = False
	While boolFound = False
		strQuery = "SELECT aDSPath FROM 'LDAP://" & strLDAP_Server & "' WHERE objectClass='Computer' AND Name='" & strComputerPrefix & strStart & "'"
		objCommand.CommandText = strQuery
		On Error Resume Next
		Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			MsgBox "Error " & Err.Number & VbCrLf & "Description: " & Err.Description & VbCrLf & "Query: " & strQuery
			Err.Clear
			On Error GoTo 0
			strNewName = ""
			boolFound = True
		Else
			On Error GoTo 0
			If Not objRecordSet.EOF Then
				strStart = CStr(CInt(strStart) + 1)
			Else
				boolFound = True
				strNewName = strComputerPrefix & strStart
			End If
		End If
	Wend
	 
	objConnection.Close
	 
	Set objRecordSet = Nothing
	Set objCommand = Nothing
	Set objConnection = Nothing
	 
	GetNextAvailablePCName = strNewName
End Function

Open in new window

Rob i still get the permission denied error....
Rob i still get the permission denied error....
OK, another addition:
      objConnection.Properties("ADSI Flag") = ADS_SERVER_BIND

Regards,

Rob.
Option Explicit
 
Dim objNetwork, objShell, objFSO, strRenameFlag, strComputerName, strDomain, strNextAvailableName
Dim objWMIService, colComputers, objComputer, intErrorCode, objOutputFile, strCommand
Dim strAdminUser, strAdminPass
 
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strRenameFlag = "C:\Temp\Renamed.txt"
 
strComputerName = objNetwork.ComputerName
 
If objFSO.FileExists(objFSO.GetSpecialFolder(1) & "\NetDom.exe") = False Then
	If objFSO.FileExists(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe") = True Then
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe", objFSO.GetSpecialFolder(1) & "\"
		MsgBox "NetDom.exe has been copied to " & objFSO.GetSpecialFolder(1) & "\"
	Else
		MsgBox "NetDom.exe does not exist at " & objFSO.GetSpecialFolder(1) & "\NetDom.exe.  Cannot continue script."
		WScript.Quit
	End If
End If
 
If objFSO.FolderExists("C:\Temp") = False Then
	objFSO.CreateFolder("C:\Temp")
End If
 
If objFSO.FileExists(strRenameFlag) = False Then
	strNextAvailableName = GetNextAvailablePCName
	If strNextAvailableName <> "" Then
		If UCase(strComputerName) <> UCase(strNextAvailableName) Then
			' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
			'strNextAvailableName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
			MsgBox "Computer will now be renamed to " & strNextAvailableName
			Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
			Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
			For Each objComputer in colComputers
				intErrorCode = objComputer.Rename(UCase(strNextAvailableName))
				If intErrorCode <> 0 Then
					WScript.Echo "Error renaming computer. Error # " & intErrorCode
				Else
					Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
					objOutputFile.Close
					Set objOutputFile = Nothing
					WScript.Echo "Computer renamed. It will now reboot. Please re-run this script again after logging in."
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					objShell.Run "shutdown -r -t 00", 1, False
				End If
			Next
		Else
			WScript.Echo "Computer already has the correct name."
			Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
			objOutputFile.Close
			Set objOutputFile = Nothing
			' Run the script again to have it joined to the domain.
			objShell.Run "wscript """ & WScript.ScriptFullName & """", 1, False
		End If
	Else
		WScript.Echo "Could not find the next available computer name."
	End If
Else
	' IF THE COMPUTER HAS BEEN RENAMED, THEN IT WILL JOIN THE DOMAIN
	'Alternate Join Computer code: http://msdn2.microsoft.com/en-us/library/aa392154.aspx
	objShell.Run "control timedate.cpl ,1", 1, False
	WScript.Echo "Please select the ""Time Zone"" tab and select your time zone, set your time, then click OK."
	strDomain = InputBox("Enter the domain that you want to join:", "Domain Name", "DefaultDomain")
	strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
	'strAdminUser = "Administrator"
	'strAdminPass = "*"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
	WScript.Echo strCommand
	'objShell.Run strCommand, 1, False
	
	' Now we need to stop the script from running again.
	'strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""wscript.exe " & WScript.ScriptFullName & """ /f"
	'objShell.Run strCommand, 0, True
	objFSO.DeleteFile strRenameFlag, True
	'objFSO.DeleteFile WScript.ScriptFullName, True
	
	WScript.Echo "Your computer will be joined to the domain shortly, and reboot automatically. Please wait."
End If
 
Function GetNextAvailablePCName
	' These are required, they are only implicit when pre-authenticated against the domain.
 
	Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
	Const strDomainName = "yourdomain"
	Const strUsername = "username"
	Const strPassword = "password"
	Const strLDAP_Server = "maindc.domain.com"
	Const strComputerPrefix = "DEV-CHEN-PC"
	strStart = "4001"
 
	Const ADS_SECURE_AUTHENTICATION = 1
	Const ADS_USE_ENCRYPTION = 2
	Const ADS_CHASE_REFERRALS_EXTERNAL = &H40
	Const ADS_SERVER_BIND = &h200
 
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADSDSOObject"
 
	' Authenticate ADODB Connection
	objConnection.Properties("User ID") = strDomainName & "\" & strUsername
	objConnection.Properties("Password") = strPassword
	objConnection.Properties("ADSI Flag") = ADS_SERVER_BIND
 
	objConnection.Open "Active Directory Provider"
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Timeout") = 600
	objCommand.Properties("Searchscope") = 2
	objCommand.Properties("Cache Results") = False
	objCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL
	 
	' Create a simple LDAP Query for computer name
	boolFound = False
	While boolFound = False
		strQuery = "SELECT aDSPath FROM 'LDAP://" & strLDAP_Server & "' WHERE objectClass='Computer' AND Name='" & strComputerPrefix & strStart & "'"
		objCommand.CommandText = strQuery
		On Error Resume Next
		Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			MsgBox "Error " & Err.Number & VbCrLf & "Description: " & Err.Description & VbCrLf & "Query: " & strQuery
			Err.Clear
			On Error GoTo 0
			strNewName = ""
			boolFound = True
		Else
			On Error GoTo 0
			If Not objRecordSet.EOF Then
				strStart = CStr(CInt(strStart) + 1)
			Else
				boolFound = True
				strNewName = strComputerPrefix & strStart
			End If
		End If
	Wend
	 
	objConnection.Close
	 
	Set objRecordSet = Nothing
	Set objCommand = Nothing
	Set objConnection = Nothing
	 
	GetNextAvailablePCName = strNewName
End Function

Open in new window

Rob still the same error.
Rob still the same error.
Hmmm, try setting
Const strDomainName = "???"

to the friendly domain name.

For example, if I have:
Const strDomainName = "mydomain.local"

I get "Permission Denied", but if I have:
Const strDomainName = "mydomainname"

it works.  Although I haven't tested with a machine that is *not* on the domain.

My feeling is that this is a trust issue between a non-domain PC.....

See how you go if you try that...

Rob.
Oh, on your computer that *is* on the domain, if you type this at a command prompt:
echo %userdomain%

you'll get the "friendly" name.

Rob.
Rob The machine renaming works now...
Can we hardcode the domain name within it and without a restart can it change the name and add to domain in 1 shot.
Should i mention the machine name within it for each machine?
After 1st restart when run script i get this

---------------------------
Windows Script Host
---------------------------
Please select the "Time Zone" tab and select your time zone, set your time, then click OK.
---------------------------
OK  
---------------------------

Can all this be done in 1 restart and
I get this box

---------------------------
Windows Script Host
---------------------------
Your computer will be joined to the domain shortly, and reboot automatically. Please wait.
---------------------------
OK  
---------------------------

and nothing happens
Rob The machine renaming works now...
Can we hardcode the domain name within it and without a restart can it change the name and add to domain in 1 shot.
Should i mention the machine name within it for each machine?
After 1st restart when run script i get this

---------------------------
Windows Script Host
---------------------------
Please select the "Time Zone" tab and select your time zone, set your time, then click OK.
---------------------------
OK  
---------------------------

Can all this be done in 1 restart and
I get this box

---------------------------
Windows Script Host
---------------------------
Your computer will be joined to the domain shortly, and reboot automatically. Please wait.
---------------------------
OK  
---------------------------

and nothing happens
OK, great...

To answer your question about doing it all at once, no, this cannot be done without a reboot, because the system needs to have its new name registered properly before joining the domain.

>> Can we hardcode the domain name
Change this:
strDomain = InputBox("Enter the domain that you want to join:", "Domain Name", "DefaultDomain")

to this
strDomain = "mydomain"

>> Should i mention the machine name within it for each machine?

In theory, no.  The first pc would have been name DEV-CHEN-PC4001, so on the next pc (as long as the first succesfully joined the domain), it should detect 4001 already exists, and rename the new computer to DEV-CHEN-PC4002, and so on.

>> Your computer will be joined to the domain shortly
>> Nothing happens

For this bit, it would have shown you a message box like
cmd /k NETDOM JOIN DEV-CHEN-PC4001 /Domain:mydomain /REBOOT 10

but that command didn't actually execute.  You will see these two lines:
      WScript.Echo strCommand
      'objShell.Run strCommand, 1, False

change that to
      WScript.Echo "Executing " & strCommand
      objShell.Run strCommand, 1, False

and you will see the output of the NetDom command.  To close the DOS prompt, type Exit.

If the NetDom command executes succesfully, change this line:
      strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"

to this
      strCommand = "cmd /c NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"

Regards,

Rob.
Rob now i get this...

---------------------------
C:\Netdom.exe
---------------------------
C:\Netdom.exe is not a valid Win32 application.


---------------------------
OK  
---------------------------

I am running this on a win 2003 machine.
Rob now i get this...

---------------------------
C:\Netdom.exe
---------------------------
C:\Netdom.exe is not a valid Win32 application.


---------------------------
OK  
---------------------------

I am running this on a win 2003 machine.
Hmmm, maybe that's not the correct version of NetDom....I have one on a Win2003 Server in this location:
C:\Program Files\Support Tools\netdom.exe

See if you have that, and copy that over the System32 version (back it up first).

Regards,

Rob.
Rob i dont have the folder either...
Can we have a shared folder mentioned in the script that can get the required file from the folder to the system32 folder?
Rob i dont have the folder either...
Can we have a shared folder mentioned in the script that can get the required file from the folder to the system32 folder?
As long as you've got a valid NetDom.exe that will run on your computer, you can have it with your script file on the USB stick, and it will use that copy.  You just need to find a version that will work for you......just grab one, type
<path>netdom /?

where <path> is the path to that file, and if you don't get
Netdom.exe is not a valid Win32 application

it should be fine.

Regards,

Rob.
I get as its an invalid file.
Where can i download it from Rob .Is any version ok
I get as its an invalid file.
Where can i download it from Rob .Is any version ok
Try the file from my Windows 2003 Server....seems to work fine on XP as well....
It's not actually a ZIP file, just rename it from NetDom.zip to NetDom.exe

Rob.
netdom.zip
ROB THIS FILE WORKS NOW I GET AS THE USERNAME AND PASSWORD IS INCORRECT

Const strDomainName = "development"
Const strUsername = "Administrator"
Const strPassword = "****"
Const strLDAP_Server = "in01.development.group.co.uk"
Const strComputerPrefix = "dev-chen-pc"
strStart = "4001"
ROB THIS FILE WORKS NOW I GET AS THE USERNAME AND PASSWORD IS INCORRECT

Const strDomainName = "development"
Const strUsername = "Administrator"
Const strPassword = "****"
Const strLDAP_Server = "in01.development.group.co.uk"
Const strComputerPrefix = "dev-chen-pc"
strStart = "4001"
Rob i change this line
   strCommand = "cmd /c NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"

After which i dont get any error but just get this and nothing happens.

---------------------------
Windows Script Host
---------------------------
Your computer will be joined to the domain shortly, and reboot automatically. Please wait.
---------------------------
OK  
---------------------------

If i change this
Const strDomainName = "development"
to this
Const strDomainName = "development.local.co.uk"
or
Const strDomainName = "current machine name"

I get a permission denied error...
Rob i change this line
   strCommand = "cmd /c NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"

After which i dont get any error but just get this and nothing happens.

---------------------------
Windows Script Host
---------------------------
Your computer will be joined to the domain shortly, and reboot automatically. Please wait.
---------------------------
OK  
---------------------------

If i change this
Const strDomainName = "development"
to this
Const strDomainName = "development.local.co.uk"
or
Const strDomainName = "current machine name"

I get a permission denied error...
OK, good, so we're nearly there.....in the script, you'll have this section:

      strDomain = "development"
      strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
      'strAdminUser = "Administrator"
      'strAdminPass = "*"
      'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
      'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
      WScript.Echo strCommand
      'objShell.Run strCommand, 1, False



well, the last line is commented out, which is what actually runs the command, so uncomment that, and see what the output of that command is when the command prompt appears.....

Rob.
Rob can we remove the below box

---------------------------
Windows Script Host
---------------------------
Please select the "Time Zone" tab and select your time zone, set your time, then click OK.
---------------------------
OK  
---------------------------
After the above i get this
---------------------------
Windows Script Host
---------------------------
Executing cmd /c NETDOM JOIN DEV-CHEN-PC4001 /Domain:development /REBOOT 10
---------------------------
OK  
---------------------------
Then this
---------------------------
Windows Script Host
---------------------------
Your computer will be joined to the domain shortly, and reboot automatically. Please wait.
---------------------------
OK  
---------------------------
And nothing happens....

I ahve uncommented the below line

WScript.Echo strCommand
      'objShell.Run strCommand, 1, False
Rob can we remove the below box

---------------------------
Windows Script Host
---------------------------
Please select the "Time Zone" tab and select your time zone, set your time, then click OK.
---------------------------
OK  
---------------------------
After the above i get this
---------------------------
Windows Script Host
---------------------------
Executing cmd /c NETDOM JOIN DEV-CHEN-PC4001 /Domain:development /REBOOT 10
---------------------------
OK  
---------------------------
Then this
---------------------------
Windows Script Host
---------------------------
Your computer will be joined to the domain shortly, and reboot automatically. Please wait.
---------------------------
OK  
---------------------------
And nothing happens....

I ahve uncommented the below line

WScript.Echo strCommand
      'objShell.Run strCommand, 1, False
Try this....hopefully we can see the output of the NetDom command...

Rob.
' Source: http://www.rlmueller.net/ADOAltCredentials.htm
Option Explicit
 
Dim objNetwork, objShell, objFSO, strRenameFlag, strComputerName, strDomain, strNextAvailableName
Dim objWMIService, colComputers, objComputer, intErrorCode, objOutputFile, strCommand
Dim strAdminUser, strAdminPass
 
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strRenameFlag = "C:\Temp\Renamed.txt"
 
strComputerName = objNetwork.ComputerName
 
If objFSO.FileExists(objFSO.GetSpecialFolder(1) & "\NetDom.exe") = False Then
	If objFSO.FileExists(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe") = True Then
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe", objFSO.GetSpecialFolder(1) & "\"
		WScript.Echo "NetDom.exe has been copied to " & objFSO.GetSpecialFolder(1) & "\"
	Else
		WScript.Echo "NetDom.exe does not exist at " & objFSO.GetSpecialFolder(1) & "\NetDom.exe.  Cannot continue script."
		WScript.Quit
	End If
End If
 
If objFSO.FolderExists("C:\Temp") = False Then
	objFSO.CreateFolder("C:\Temp")
End If
 
If objFSO.FileExists(strRenameFlag) = False Then
	strNextAvailableName = GetNextAvailablePCName
	If strNextAvailableName <> "" Then
		If UCase(strComputerName) <> UCase(strNextAvailableName) Then
			' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
			'strNextAvailableName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
			WScript.Echo "Computer will now be renamed to " & strNextAvailableName
			Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
			Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
			For Each objComputer in colComputers
				intErrorCode = objComputer.Rename(UCase(strNextAvailableName))
				If intErrorCode <> 0 Then
					WScript.Echo "Error renaming computer. Error # " & intErrorCode
				Else
					Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
					objOutputFile.Close
					Set objOutputFile = Nothing
					WScript.Echo "Computer renamed. It will now reboot. Please re-run this script again after logging in."
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					objShell.Run "shutdown -r -t 00", 1, False
				End If
			Next
		Else
			WScript.Echo "Computer already has the correct name."
			Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
			objOutputFile.Close
			Set objOutputFile = Nothing
			' Run the script again to have it joined to the domain.
			objShell.Run "wscript """ & WScript.ScriptFullName & """", 1, False
		End If
	Else
		WScript.Echo "Could not find the next available computer name."
	End If
Else
	' IF THE COMPUTER HAS BEEN RENAMED, THEN IT WILL JOIN THE DOMAIN
	'Alternate Join Computer code: http://msdn2.microsoft.com/en-us/library/aa392154.aspx
	strDomain = "Development"
	strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
	'strAdminUser = "Administrator"
	'strAdminPass = "*"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
	'WScript.Echo strCommand
	objShell.Run strCommand, 1, False
	
	' Now we need to stop the script from running again.
	'strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""wscript.exe " & WScript.ScriptFullName & """ /f"
	'objShell.Run strCommand, 0, True
	objFSO.DeleteFile strRenameFlag, True
	'objFSO.DeleteFile WScript.ScriptFullName, True
	
	WScript.Echo "Your computer will be joined to the domain shortly, and reboot automatically. Please wait."
End If
 
Function GetNextAvailablePCName
	' These are required, they are only implicit when pre-authenticated against the domain.
 
	Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
	Const strDomainName = "domain"
	Const strUsername = "username"
	Const strPassword = "password"
	Const strLDAP_Server = "maindc.domain.com"
	Const strComputerPrefix = "DEV-CHEN-PC"
	strStart = "4001"
 
	Const ADS_SECURE_AUTHENTICATION = &H1
	Const ADS_USE_ENCRYPTION = &H2
	Const ADS_CHASE_REFERRALS_EXTERNAL = &H40
	Const ADS_SERVER_BIND = &h200
 
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADSDSOObject"
 
	' Authenticate ADODB Connection
	objConnection.Properties("User ID") = strDomainName & "\" & strUsername
	objConnection.Properties("Password") = strPassword
	objConnection.Properties("ADSI Flag") = ADS_SECURE_AUTHENTICATION + ADS_USE_ENCRYPTION + ADS_SERVER_BIND
 
	objConnection.Open "Active Directory Provider"
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Timeout") = 600
	objCommand.Properties("Searchscope") = 2
	objCommand.Properties("Cache Results") = False
	objCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL
	 
	' Create a simple LDAP Query for computer name
	boolFound = False
	While boolFound = False
		strQuery = "SELECT aDSPath FROM 'LDAP://" & strLDAP_Server & "' WHERE objectClass='Computer' AND Name='" & strComputerPrefix & strStart & "'"
		objCommand.CommandText = strQuery
		On Error Resume Next
		Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			WScript.Echo "Error " & Err.Number & VbCrLf & "Description: " & Err.Description & VbCrLf & "Query: " & strQuery
			Err.Clear
			On Error GoTo 0
			strNewName = ""
			boolFound = True
		Else
			On Error GoTo 0
			If Not objRecordSet.EOF Then
				strStart = CStr(CInt(strStart) + 1)
			Else
				boolFound = True
				strNewName = strComputerPrefix & strStart
			End If
		End If
	Wend
	 
	objConnection.Close
	 
	Set objRecordSet = Nothing
	Set objCommand = Nothing
	Set objConnection = Nothing
	 
	GetNextAvailablePCName = strNewName
End Function

Open in new window

Rob i get this

---------------------------
Windows Script Host
---------------------------
Error -2147217865

Description: Table does not exist.

Query: SELECT aDSPath FROM 'LDAP://development.group.co.uk' WHERE objectClass='Computer' AND Name='DEV-CHEN-PC4001'
---------------------------
OK  
---------------------------
Rob i get this

---------------------------
Windows Script Host
---------------------------
Error -2147217865

Description: Table does not exist.

Query: SELECT aDSPath FROM 'LDAP://development.group.co.uk' WHERE objectClass='Computer' AND Name='DEV-CHEN-PC4001'
---------------------------
OK  
---------------------------
You had that a while ago....make sure that
Const strLDAP_Server = "maindc.domain.com"

is set correctly....

Rob.
Yes Rob this part is set right...Can you confirm

Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
      Const strDomainName = "development"
      Const strUsername = "administrator"
      Const strPassword = "Pass"
      Const strLDAP_Server = "development.group.co.uk"
      Const strComputerPrefix = "DEV-CHEN-PC"
      strStart = "4001"
Yes Rob this part is set right...Can you confirm

Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
      Const strDomainName = "development"
      Const strUsername = "administrator"
      Const strPassword = "Pass"
      Const strLDAP_Server = "development.group.co.uk"
      Const strComputerPrefix = "DEV-CHEN-PC"
      strStart = "4001"
Is "development.group.co.uk" the name of the domain controller?  I thought it was "inssrv01.development.group.co.uk"

Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
      Const strDomainName = "development"
      Const strUsername = "administrator"
      Const strPassword = "Pass"
      Const strLDAP_Server = "inssrv01.development.group.co.uk"
      Const strComputerPrefix = "DEV-CHEN-PC"
      strStart = "4001"


Regards,

Rob.
I even tried that before Rob but same error

---------------------------
Windows Script Host
---------------------------
Error -2147217865

Description: Table does not exist.

Query: SELECT aDSPath FROM 'LDAP://inssrv01.development.group.co.uk' WHERE objectClass='Computer' AND Name='DEV-CHEN-PC4001'
---------------------------
OK  
---------------------------
I even tried that before Rob but same error

---------------------------
Windows Script Host
---------------------------
Error -2147217865

Description: Table does not exist.

Query: SELECT aDSPath FROM 'LDAP://inssrv01.development.group.co.uk' WHERE objectClass='Computer' AND Name='DEV-CHEN-PC4001'
---------------------------
OK  
---------------------------
Try changing this line:
      objConnection.Properties("ADSI Flag") = ADS_SECURE_AUTHENTICATION + ADS_USE_ENCRYPTION + ADS_SERVER_BIND

to this
      objConnection.Properties("ADSI Flag") = ADS_SERVER_BIND

Rob.
Rob i get this now...

Logon failure: unknown user name or bad password.

The command failed to complete successfully.

Rob i get this now...

Logon failure: unknown user name or bad password.

The command failed to complete successfully.

Rob is some issue that i have the DC's on a different OU
No, that sounds like you're now at the NetDom command output.  Is that in the command prompt, where you have to type Exit to close it?

This time, try using the lower NetDom command, which I've uncommented for you.

So where you have this:

      strDomain = "Development"
      'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
      strAdminUser = "Administrator"
      strAdminPass = "*"
      strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
      'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
      'WScript.Echo strCommand
      objShell.Run strCommand, 1, False


make sure that these three are correct:
strDomain
strAdminUser
strAdminPass


Regards,

Rob.
' Source: http://www.rlmueller.net/ADOAltCredentials.htm
Option Explicit
 
Dim objNetwork, objShell, objFSO, strRenameFlag, strComputerName, strDomain, strNextAvailableName
Dim objWMIService, colComputers, objComputer, intErrorCode, objOutputFile, strCommand
Dim strAdminUser, strAdminPass
 
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strRenameFlag = "C:\Temp\Renamed.txt"
 
strComputerName = objNetwork.ComputerName
 
If objFSO.FileExists(objFSO.GetSpecialFolder(1) & "\NetDom.exe") = False Then
	If objFSO.FileExists(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe") = True Then
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe", objFSO.GetSpecialFolder(1) & "\"
		WScript.Echo "NetDom.exe has been copied to " & objFSO.GetSpecialFolder(1) & "\"
	Else
		WScript.Echo "NetDom.exe does not exist at " & objFSO.GetSpecialFolder(1) & "\NetDom.exe.  Cannot continue script."
		WScript.Quit
	End If
End If
 
If objFSO.FolderExists("C:\Temp") = False Then
	objFSO.CreateFolder("C:\Temp")
End If
 
If objFSO.FileExists(strRenameFlag) = False Then
	strNextAvailableName = GetNextAvailablePCName
	If strNextAvailableName <> "" Then
		If UCase(strComputerName) <> UCase(strNextAvailableName) Then
			' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
			'strNextAvailableName = InputBox("Enter a new machine name for this computer:", "Rename Computer")
			WScript.Echo "Computer will now be renamed to " & strNextAvailableName
			Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
			Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
			For Each objComputer in colComputers
				intErrorCode = objComputer.Rename(UCase(strNextAvailableName))
				If intErrorCode <> 0 Then
					WScript.Echo "Error renaming computer. Error # " & intErrorCode
				Else
					Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
					objOutputFile.Close
					Set objOutputFile = Nothing
					WScript.Echo "Computer renamed. It will now reboot. Please re-run this script again after logging in."
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					objShell.Run "shutdown -r -t 00", 1, False
				End If
			Next
		Else
			WScript.Echo "Computer already has the correct name."
			Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
			objOutputFile.Close
			Set objOutputFile = Nothing
			' Run the script again to have it joined to the domain.
			objShell.Run "wscript """ & WScript.ScriptFullName & """", 1, False
		End If
	Else
		WScript.Echo "Could not find the next available computer name."
	End If
Else
	' IF THE COMPUTER HAS BEEN RENAMED, THEN IT WILL JOIN THE DOMAIN
	'Alternate Join Computer code: http://msdn2.microsoft.com/en-us/library/aa392154.aspx
	strDomain = "Development"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
	strAdminUser = "Administrator"
	strAdminPass = "*"
	strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
	'WScript.Echo strCommand
	objShell.Run strCommand, 1, False
	
	' Now we need to stop the script from running again.
	'strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""wscript.exe " & WScript.ScriptFullName & """ /f"
	'objShell.Run strCommand, 0, True
	objFSO.DeleteFile strRenameFlag, True
	'objFSO.DeleteFile WScript.ScriptFullName, True
	
	WScript.Echo "Your computer will be joined to the domain shortly, and reboot automatically. Please wait."
End If
 
Function GetNextAvailablePCName
	' These are required, they are only implicit when pre-authenticated against the domain.
 
	Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
	Const strDomainName = "domain"
	Const strUsername = "username"
	Const strPassword = "password"
	Const strLDAP_Server = "maindc.domain.com"
	Const strComputerPrefix = "DEV-CHEN-PC"
	strStart = "4001"
 
	Const ADS_SECURE_AUTHENTICATION = &H1
	Const ADS_USE_ENCRYPTION = &H2
	Const ADS_CHASE_REFERRALS_EXTERNAL = &H40
	Const ADS_SERVER_BIND = &h200
 
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADSDSOObject"
 
	' Authenticate ADODB Connection
	objConnection.Properties("User ID") = strDomainName & "\" & strUsername
	objConnection.Properties("Password") = strPassword
	objConnection.Properties("ADSI Flag") = ADS_SERVER_BIND
 
	objConnection.Open "Active Directory Provider"
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Timeout") = 600
	objCommand.Properties("Searchscope") = 2
	objCommand.Properties("Cache Results") = False
	objCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL
	 
	' Create a simple LDAP Query for computer name
	boolFound = False
	While boolFound = False
		strQuery = "SELECT aDSPath FROM 'LDAP://" & strLDAP_Server & "' WHERE objectClass='Computer' AND Name='" & strComputerPrefix & strStart & "'"
		objCommand.CommandText = strQuery
		On Error Resume Next
		Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			WScript.Echo "Error " & Err.Number & VbCrLf & "Description: " & Err.Description & VbCrLf & "Query: " & strQuery
			Err.Clear
			On Error GoTo 0
			strNewName = ""
			boolFound = True
		Else
			On Error GoTo 0
			If Not objRecordSet.EOF Then
				strStart = CStr(CInt(strStart) + 1)
			Else
				boolFound = True
				strNewName = strComputerPrefix & strStart
			End If
		End If
	Wend
	 
	objConnection.Close
	 
	Set objRecordSet = Nothing
	Set objCommand = Nothing
	Set objConnection = Nothing
	 
	GetNextAvailablePCName = strNewName
End Function

Open in new window

Now i get it Rob... :-))
It works
After entering the password manually...
Can we hardcode the password in now...
What is the changes i need to make
Now i get it Rob... :-))
It works
After entering the password manually...
Can we hardcode the password in now...
What is the changes i need to make
Need to delete the temp folder... Thats created in C Drive.
What is the best softare that you recomend to encrypt the vbs file to exe.
So its going be difficult to crack it...
Which ever is best you prefer...

Rob for machines that i come for a format. I need to assign the same name that was there before.Can i have just 1 change done that the script asks me for the machine name. So i can mention the machine name alone.
All the other credentials,Domain all is hard coded.

So at the end i shall have 2 files one for new machines that automatically get a machine name.
And another that gets the name mentioned manually

You should be able to get away with only one file....I've moved the things that you need to configure to this section at the top:

' ** CONFIGURATION **
      Const strDomain = "domain"
      Const strAdminUser = "username"
      Const strAdminPass = "password"
      Const strLDAP_Server = "maindc.domain.com"
' *******************

What it will do this time is provide an input box that says:
"This computer will be renamed to the name below."
"If you do not want to use this name, enter a new one:"
  DEV-CHEN-PC4001

and you can just press Enter to accept that as the name, or simply type a new one over it.

It also now deletes the Temp folder after joining the domain.

Also, to make EXE files out of VBS files, we bought a license for PrimalScript Professional, which has a built-in compiler in it, so we use that.

If you ever need to test the renaming of the computer again, just make sure this file doesn't exist:
C:\Temp\Renamed.txt

Regards,

Rob.
' Source: http://www.rlmueller.net/ADOAltCredentials.htm
Option Explicit
 
Dim objNetwork, objShell, objFSO, strRenameFlag, strComputerName, strDomain, strNextAvailableName
Dim objWMIService, colComputers, objComputer, intErrorCode, objOutputFile, strCommand
Dim strAdminUser, strAdminPass
 
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strRenameFlag = "C:\Temp\Renamed.txt"
 
' ** CONFIGURATION **
	Const strDomain = "domain"
	Const strAdminUser = "username"
	Const strAdminPass = "password"
	Const strLDAP_Server = "maindc.domain.com"
' *******************
 
strComputerName = objNetwork.ComputerName
 
If objFSO.FileExists(objFSO.GetSpecialFolder(1) & "\NetDom.exe") = False Then
	If objFSO.FileExists(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe") = True Then
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe", objFSO.GetSpecialFolder(1) & "\"
		WScript.Echo "NetDom.exe has been copied to " & objFSO.GetSpecialFolder(1) & "\"
	Else
		WScript.Echo "NetDom.exe does not exist at " & objFSO.GetSpecialFolder(1) & "\NetDom.exe.  Cannot continue script."
		WScript.Quit
	End If
End If
 
If objFSO.FolderExists("C:\Temp") = False Then
	objFSO.CreateFolder("C:\Temp")
End If
 
If objFSO.FileExists(strRenameFlag) = False Then
	strNextAvailableName = GetNextAvailablePCName
	If strNextAvailableName <> "" Then
		If UCase(strComputerName) <> UCase(strNextAvailableName) Then
			' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
			'WScript.Echo "Computer will now be renamed to " & strNextAvailableName
			strNextAvailableName = InputBox("This computer will be renamed to the name below." & VbCrLf & _
				"If you do not want to use this name, enter a new one:", "Rename Computer", strNextAvailableName)
			Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
			Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
			For Each objComputer in colComputers
				intErrorCode = objComputer.Rename(UCase(strNextAvailableName))
				If intErrorCode <> 0 Then
					WScript.Echo "Error renaming computer. Error # " & intErrorCode
				Else
					Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
					objOutputFile.Close
					Set objOutputFile = Nothing
					WScript.Echo "Computer renamed. It will now reboot. Please re-run this script again after logging in."
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					objShell.Run "shutdown -r -t 00", 1, False
				End If
			Next
		Else
			WScript.Echo "Computer already has the correct name."
			Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
			objOutputFile.Close
			Set objOutputFile = Nothing
			' Run the script again to have it joined to the domain.
			objShell.Run "wscript """ & WScript.ScriptFullName & """", 1, False
		End If
	Else
		WScript.Echo "Could not find the next available computer name."
	End If
Else
	' IF THE COMPUTER HAS BEEN RENAMED, THEN IT WILL JOIN THE DOMAIN
	'Alternate Join Computer code: http://msdn2.microsoft.com/en-us/library/aa392154.aspx
 
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
	strCommand = "cmd /c NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
	'WScript.Echo strCommand
	objShell.Run strCommand, 1, False
	
	' Now we need to stop the script from running again.
	'strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""wscript.exe " & WScript.ScriptFullName & """ /f"
	'objShell.Run strCommand, 0, True
	objFSO.DeleteFile strRenameFlag, True
	On Error Resume Next
	objFSO.DeleteFolder "C:\Temp", True
	Err.Clear
	On Error GoTo 0
	'objFSO.DeleteFile WScript.ScriptFullName, True
	
	WScript.Echo "Your computer will be joined to the domain shortly, and reboot automatically. Please wait."
End If
 
Function GetNextAvailablePCName
	Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
	Const strComputerPrefix = "DEV-CHEN-PC"
	strStart = "4001"
 
	Const ADS_SECURE_AUTHENTICATION = &H1
	Const ADS_USE_ENCRYPTION = &H2
	Const ADS_CHASE_REFERRALS_EXTERNAL = &H40
	Const ADS_SERVER_BIND = &h200
 
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADSDSOObject"
 
	' Authenticate ADODB Connection
	objConnection.Properties("User ID") = strDomain & "\" & strAdminUser
	objConnection.Properties("Password") = strAdminPass
	objConnection.Properties("ADSI Flag") = ADS_SERVER_BIND
 
	objConnection.Open "Active Directory Provider"
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Timeout") = 600
	objCommand.Properties("Searchscope") = 2
	objCommand.Properties("Cache Results") = False
	objCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL
	 
	' Create a simple LDAP Query for computer name
	boolFound = False
	While boolFound = False
		strQuery = "SELECT aDSPath FROM 'LDAP://" & strLDAP_Server & "' WHERE objectClass='Computer' AND Name='" & strComputerPrefix & strStart & "'"
		objCommand.CommandText = strQuery
		On Error Resume Next
		Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			WScript.Echo "Error " & Err.Number & VbCrLf & "Description: " & Err.Description & VbCrLf & "Query: " & strQuery
			Err.Clear
			On Error GoTo 0
			strNewName = ""
			boolFound = True
		Else
			On Error GoTo 0
			If Not objRecordSet.EOF Then
				strStart = CStr(CInt(strStart) + 1)
			Else
				boolFound = True
				strNewName = strComputerPrefix & strStart
			End If
		End If
	Wend
	 
	objConnection.Close
	 
	Set objRecordSet = Nothing
	Set objCommand = Nothing
	Set objConnection = Nothing
	 
	GetNextAvailablePCName = strNewName
End Function

Open in new window

Rob i get this...

---------------------------
Windows Script Host
---------------------------
Script:      C:\New.vbs
Line:      15
Char:      8
Error:      Name redefined
Code:      800A0411
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
Rob i get this...

---------------------------
Windows Script Host
---------------------------
Script:      C:\New.vbs
Line:      15
Char:      8
Error:      Name redefined
Code:      800A0411
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
Oh, that always happens when I move stuff! Sorry....
' Source: http://www.rlmueller.net/ADOAltCredentials.htm
Option Explicit
 
Dim objNetwork, objShell, objFSO, strRenameFlag, strComputerName, strNextAvailableName
Dim objWMIService, colComputers, objComputer, intErrorCode, objOutputFile, strCommand
Dim strAdminUser, strAdminPass
 
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strRenameFlag = "C:\Temp\Renamed.txt"
 
' ** CONFIGURATION **
	Const strDomain = "domain"
	Const strAdminUser = "username"
	Const strAdminPass = "password"
	Const strLDAP_Server = "maindc.domain.com"
' *******************
 
strComputerName = objNetwork.ComputerName
 
If objFSO.FileExists(objFSO.GetSpecialFolder(1) & "\NetDom.exe") = False Then
	If objFSO.FileExists(Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe") = True Then
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "NetDom.exe", objFSO.GetSpecialFolder(1) & "\"
		WScript.Echo "NetDom.exe has been copied to " & objFSO.GetSpecialFolder(1) & "\"
	Else
		WScript.Echo "NetDom.exe does not exist at " & objFSO.GetSpecialFolder(1) & "\NetDom.exe.  Cannot continue script."
		WScript.Quit
	End If
End If
 
If objFSO.FolderExists("C:\Temp") = False Then
	objFSO.CreateFolder("C:\Temp")
End If
 
If objFSO.FileExists(strRenameFlag) = False Then
	strNextAvailableName = GetNextAvailablePCName
	If strNextAvailableName <> "" Then
		If UCase(strComputerName) <> UCase(strNextAvailableName) Then
			' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
			'WScript.Echo "Computer will now be renamed to " & strNextAvailableName
			strNextAvailableName = InputBox("This computer will be renamed to the name below." & VbCrLf & _
				"If you do not want to use this name, enter a new one:", "Rename Computer", strNextAvailableName)
			Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
			Set colComputers = objWMIService.ExecQuery ("Select Name from Win32_ComputerSystem")
			For Each objComputer in colComputers
				intErrorCode = objComputer.Rename(UCase(strNextAvailableName))
				If intErrorCode <> 0 Then
					WScript.Echo "Error renaming computer. Error # " & intErrorCode
				Else
					Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
					objOutputFile.Close
					Set objOutputFile = Nothing
					WScript.Echo "Computer renamed. It will now reboot. Please re-run this script again after logging in."
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					objShell.Run "shutdown -r -t 00", 1, False
				End If
			Next
		Else
			WScript.Echo "Computer already has the correct name."
			Set objOutputFile = objFSO.CreateTextFile(strRenameFlag, True)
			objOutputFile.Close
			Set objOutputFile = Nothing
			' Run the script again to have it joined to the domain.
			objShell.Run "wscript """ & WScript.ScriptFullName & """", 1, False
		End If
	Else
		WScript.Echo "Could not find the next available computer name."
	End If
Else
	' IF THE COMPUTER HAS BEEN RENAMED, THEN IT WILL JOIN THE DOMAIN
	'Alternate Join Computer code: http://msdn2.microsoft.com/en-us/library/aa392154.aspx
 
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /REBOOT 10"
	strCommand = "cmd /c NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
	'strCommand = "cmd /k NETDOM JOIN " & strComputerName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /userO:" & strRemoteAdminUser & " /passwordO:" & strRemoteAdminPass & " /REBOOT 10"
	'WScript.Echo strCommand
	objShell.Run strCommand, 1, False
	
	' Now we need to stop the script from running again.
	'strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""wscript.exe " & WScript.ScriptFullName & """ /f"
	'objShell.Run strCommand, 0, True
	objFSO.DeleteFile strRenameFlag, True
	On Error Resume Next
	objFSO.DeleteFolder "C:\Temp", True
	Err.Clear
	On Error GoTo 0
	'objFSO.DeleteFile WScript.ScriptFullName, True
	
	WScript.Echo "Your computer will be joined to the domain shortly, and reboot automatically. Please wait."
End If
 
Function GetNextAvailablePCName
	Dim strStart, objConnection, objCommand, objRecordset, boolFound, strQuery, strNewName
	Const strComputerPrefix = "DEV-CHEN-PC"
	strStart = "4001"
 
	Const ADS_SECURE_AUTHENTICATION = &H1
	Const ADS_USE_ENCRYPTION = &H2
	Const ADS_CHASE_REFERRALS_EXTERNAL = &H40
	Const ADS_SERVER_BIND = &h200
 
	Set objConnection = CreateObject("ADODB.Connection")
	objConnection.Provider = "ADSDSOObject"
 
	' Authenticate ADODB Connection
	objConnection.Properties("User ID") = strDomain & "\" & strAdminUser
	objConnection.Properties("Password") = strAdminPass
	objConnection.Properties("ADSI Flag") = ADS_SERVER_BIND
 
	objConnection.Open "Active Directory Provider"
	Set objCommand = CreateObject("ADODB.Command")
	objCommand.ActiveConnection = objConnection
	objCommand.Properties("Page Size") = 1000
	objCommand.Properties("Timeout") = 600
	objCommand.Properties("Searchscope") = 2
	objCommand.Properties("Cache Results") = False
	objCommand.Properties("Chase Referrals") = ADS_CHASE_REFERRALS_EXTERNAL
	 
	' Create a simple LDAP Query for computer name
	boolFound = False
	While boolFound = False
		strQuery = "SELECT aDSPath FROM 'LDAP://" & strLDAP_Server & "' WHERE objectClass='Computer' AND Name='" & strComputerPrefix & strStart & "'"
		objCommand.CommandText = strQuery
		On Error Resume Next
		Set objRecordSet = objCommand.Execute
		If Err.Number <> 0 Then
			WScript.Echo "Error " & Err.Number & VbCrLf & "Description: " & Err.Description & VbCrLf & "Query: " & strQuery
			Err.Clear
			On Error GoTo 0
			strNewName = ""
			boolFound = True
		Else
			On Error GoTo 0
			If Not objRecordSet.EOF Then
				strStart = CStr(CInt(strStart) + 1)
			Else
				boolFound = True
				strNewName = strComputerPrefix & strStart
			End If
		End If
	Wend
	 
	objConnection.Close
	 
	Set objRecordSet = Nothing
	Set objCommand = Nothing
	Set objConnection = Nothing
	 
	GetNextAvailablePCName = strNewName
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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
Rob

C:\>cscript New.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Computer already has the correct name.

Nothing happens after this...
Rob

C:\>cscript New.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Computer already has the correct name.

Nothing happens after this...
Thanks  Thanks Thanks
Works perfect now Rob
Helped me a lot now...
That's great.

I have actually wanted to finish developing this for a long time, but haven't been able to develop and test at the same time as rolling out PCs, so having you test it for me has been a great help!

Regards,

Rob.
Thanks Rob....

Do you have time for couple of posts today?
I don't think I will.....not to finish them at least....we have a two hour meeting scheduled this afternoon....

I'll see if I can start on one....

Rob.
Rob one question on this post...
Can't we have just 1 restart within which both machine name change and domain addition can take effect.
Manuallyu when we do.
We can rename the machine and add the machine to domain.
Can't we do the same in this case.
Rob one question on this post...
Can't we have just 1 restart within which both machine name change and domain addition can take effect.
Manuallyu when we do.
We can rename the machine and add the machine to domain.
Can't we do the same in this case.
Any view on this Rob
Ron here is a new post for the adding

https://www.experts-exchange.com/questions/23506668/Need-to-add-the-option-of-adding-the-machine-to-the-Domain-in-one-restart-Need-addition-functionality.html

1 restart to add the machine to the domain and rename the machine...Please have a look
Rob,

I've run the script and it works ok i guess to line 122. It stops running as i do not have an LDAP server. Please can you help?

Bennon.