• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

Adding the system to Domain with a new Hostname

This is going to be a big e...
I have a VBScript, which extracts the service tags from the system (All are dell systems) and displays on the screen, we have a excel file which contains all the service tags and hostnames (eg. two colums; one with Service Tag & other with Hostname, we have read permission to that file)
I want  a VBScript that will take that service tag as a input and pull the hostname from the excel file and then assign it to the system.
Later, i have a VBScript to adds the system to Doamin, we have to integrate this script with the previous one.

Please help me doing this.
0
Dhiraj Mutha
Asked:
Dhiraj Mutha
  • 27
  • 17
1 Solution
 
RobSampsonCommented:
Hi, to rename the computer based on the values in an Excel spreadsheet, you can use this code.

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"
 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each objItem In colItems
      strSerial = objItem.SerialNumber
Next
 
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
Set objSheet = objWB.Sheets(1)
strNewName = ""
For intRow = 2 To objSheet.Cells(65536, "A").End(xlUp).Row
	If UCase(objSheet.Cells(intRow, "A").Value) = UCase(strSerial) Then
		strNewName = UCase(objSheet.Cells(intRow, "B").Value)
		Exit For
	End If
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
 
If strNewName <> "" Then
	If UCase(strNewName) <> UCase(strComputerName) Then
		' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
		'WScript.Echo "Computer will now be renamed to " & strNewName
		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(strNewName))
			If intErrorCode <> 0 Then
				WScript.Echo "Error renaming computer. Error # " & intErrorCode
			Else
				WScript.Echo "Computer renamed. It will now reboot."
				'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."
	End If
Else
	MsgBox "Could not find " & strSerial & " in column A of " & strExcelFile
End If

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
My excel file is in network path. Will this code work with it.
0
 
Dhiraj MuthaLevel DAuthor Commented:
Also let me know what should be the format of the file i.e. how the columns should be named and all.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RobSampsonCommented:
Hi, yes, you can change this:
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"

to this
strExcelFile = "\\server\share\Serials_Hostnames.xls"

or
strExcelFile = "F:\Excel\Serials_Hostnames.xls"

and the format of the spreadsheet just has headers in Row 1 of
SERIAL NUMBER              |                NEW PC NAME
111222                             |                  FINANCEPC1

Regards,

Rob.
0
 
Dhiraj MuthaLevel DAuthor Commented:
I will try this and get back to you. One small clarification: This will search the serial no and then give the host name to the system e.g: If serial no is 111222 the it will give the hostname as financepc1.
0
 
RobSampsonCommented:
That's correct.  It gets the serial number from the computer that it is currently run from, and will return the value from column B when it finds a match in column A, and set the computername to column B.

Regards,

Rob.
0
 
Dhiraj MuthaLevel DAuthor Commented:
K. Will have a check today and get back on this.
0
 
Dhiraj MuthaLevel DAuthor Commented:
Thats great... its working. A quick question, Is this script only searching the particular columns or we can place the service tag column anywere in the xl file. beacuse we have lots of other information in the file.
2) Next is to add the system to domain and restart with the same script.
0
 
Dhiraj MuthaLevel DAuthor Commented:


The script is given me an error:

Line: 40
Char: 5
Error: Object required 'objshell'
Code: 800A01A8
Source: microsoft VBScript runtime error
0
 
Dhiraj MuthaLevel DAuthor Commented:
Resolved the error: just add the following line.
Set objShell = CreateObject("Wscript.Shell")
0
 
RobSampsonCommented:
Hi.  I'm not sure if the computer can join the domain before a reboot, but give this a shot.

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"
 
' ** CONFIGURATION **
	Const strDomain = "domain"
	Const strAdminUser = "username"
	Const strAdminPass = "password"
	Const strLDAP_Server = "your.domain.com"
' *******************
 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each objItem In colItems
      strSerial = objItem.SerialNumber
Next
 
Set objShell = CreateObject("WScript.Shell")
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
Set objSheet = objWB.Sheets(1)
strNewName = ""
For intRow = 2 To objSheet.Cells(65536, "A").End(xlUp).Row
	If UCase(objSheet.Cells(intRow, "A").Value) = UCase(strSerial) Then
		strNewName = UCase(objSheet.Cells(intRow, "B").Value)
		Exit For
	End If
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
 
If strNewName <> "" Then
	If UCase(strNewName) <> UCase(strComputerName) Then
		' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
		'WScript.Echo "Computer will now be renamed to " & strNewName
		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(strNewName))
			If intErrorCode <> 0 Then
				WScript.Echo "Error renaming computer. Error # " & intErrorCode
			Else
				'WScript.Echo "Computer renamed. It will now reboot."
				'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
				'objShell.Run "shutdown -r -t 00", 1, False
				
				WScript.Echo "Computer renamed. It will now attempt to join to the domain."
				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
				
				strCommand = "cmd /c NETDOM JOIN " & strNewName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
				objShell.Run strCommand, 1, False
				
			End If
		Next
	Else
		WScript.Echo "Computer already has the correct name."
	End If
Else
	MsgBox "Could not find " & strSerial & " in column A of " & strExcelFile
End If

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
I will sure try this on tuesday, as i am travelling out of station. Thansk for the quick help.
0
 
Dhiraj MuthaLevel DAuthor Commented:
I m back now. Will have a check and update tomorrow.
0
 
Dhiraj MuthaLevel DAuthor Commented:

I tried the script.... Its able to rename the host but while adding it to the domain it gives the following error:
Line: 51
Char: 5
Error: Object required: 'objFSO'
Code: 800A01A8
Source: Microsoft VBScript runtime error
0
 
Dhiraj MuthaLevel DAuthor Commented:
Hi,

Cant we integrate this script with the previous one.

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 = "Domain"
strPassword = "Password"
strUser = "UserID"
 
Set objNetwork = CreateObject("WScript.Network")
'strComputer = objNetwork.ComputerName
 
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)
Add-to-Domain.txt
0
 
RobSampsonCommented:
Hi, you can either try my code again, but put this:
Set objFSO = CreateObject("Scripting.FileSystemObject")
above this line:
WScript.Echo "Computer renamed. It will now attempt to join to the domain."

Or, this is with your above code instead......

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"
 
' ** CONFIGURATION **
	Const strDomain = "domain"
	Const strAdminUser = "username"
	Const strAdminPass = "password"
	Const strLDAP_Server = "your.domain.com"
	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
' *******************
 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each objItem In colItems
      strSerial = objItem.SerialNumber
Next
 
Set objShell = CreateObject("WScript.Shell")
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
Set objSheet = objWB.Sheets(1)
strNewName = ""
For intRow = 2 To objSheet.Cells(65536, "A").End(xlUp).Row
	If UCase(objSheet.Cells(intRow, "A").Value) = UCase(strSerial) Then
		strNewName = UCase(objSheet.Cells(intRow, "B").Value)
		Exit For
	End If
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
 
If strNewName <> "" Then
	If UCase(strNewName) <> UCase(strComputerName) Then
		' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
		'WScript.Echo "Computer will now be renamed to " & strNewName
		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(strNewName))
			If intErrorCode <> 0 Then
				WScript.Echo "Error renaming computer. Error # " & intErrorCode
			Else
				'WScript.Echo "Computer renamed. It will now reboot."
				'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
				'objShell.Run "shutdown -r -t 00", 1, False
				
				WScript.Echo "Computer renamed. It will now attempt to join to the domain."
				 
				'strDomain = "Domain"
				'strAdminPass = "Password"
				'strAdminUser = "UserID"
				 
				Set objNetwork = CreateObject("WScript.Network")
				'strComputer = objNetwork.ComputerName
				 
				Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
				    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
				        strComputer & "'")
				 
				ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
				    strAdminPass, strDomain & "\" & strAdminUser, NULL, _
				        JOIN_DOMAIN + ACCT_CREATE)
			End If
		Next
	Else
		WScript.Echo "Computer already has the correct name."
	End If
Else
	MsgBox "Could not find " & strSerial & " in column A of " & strExcelFile
End If

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
I will try it in sometime.
0
 
RobSampsonCommented:
No problem....try either one, or both.....let me know....

Rob.
0
 
Dhiraj MuthaLevel DAuthor Commented:
1st Script - Yours - Its giving me a error that NetDom file does not exsists. I have to do this for 3000 systems, so its difficult for me to copy them on all the systems.

2nd Script - Mine - It Renams the computer but throws the following error:

Line: 68
Char: 5
Error: 0x80041021
Code: 80041021
Source: (null)
0
 
RobSampsonCommented:
Hi, you can try mine again, putting NetDom.exe in the same folder as the script file, so that it should copy it to the machine if it cannot find it there.

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"
 
' ** CONFIGURATION **
	Const strDomain = "domain"
	Const strAdminUser = "username"
	Const strAdminPass = "password"
	Const strLDAP_Server = "your.domain.com"
' *******************
 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each objItem In colItems
      strSerial = objItem.SerialNumber
Next
 
Set objShell = CreateObject("WScript.Shell")
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
Set objSheet = objWB.Sheets(1)
strNewName = ""
For intRow = 2 To objSheet.Cells(65536, "A").End(xlUp).Row
	If UCase(objSheet.Cells(intRow, "A").Value) = UCase(strSerial) Then
		strNewName = UCase(objSheet.Cells(intRow, "B").Value)
		Exit For
	End If
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
 
Set objNetwork = CreateObject("WScript.Network")
strComputerName = objNetwork.ComputerName
 
If strNewName <> "" Then
	If UCase(strNewName) <> UCase(strComputerName) Then
		' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
		'WScript.Echo "Computer will now be renamed to " & strNewName
		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(strNewName))
			If intErrorCode <> 0 Then
				WScript.Echo "Error renaming computer. Error # " & intErrorCode
			Else
				'WScript.Echo "Computer renamed. It will now reboot."
				'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
				'objShell.Run "shutdown -r -t 00", 1, False
				
				Set objFSO = CreateObject("Scripting.FileSystemObject")
				WScript.Echo "Computer renamed. It will now attempt to join to the domain."
				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
				
				strCommand = "cmd /c NETDOM JOIN " & strNewName & " /Domain:" & strDomain & " /userD:" & strDomain & "\" & strAdminUser & " /passwordD:" & strAdminPass & " /REBOOT 10"
				objShell.Run strCommand, 1, False
				
			End If
		Next
	Else
		WScript.Echo "Computer already has the correct name."
	End If
Else
	MsgBox "Could not find " & strSerial & " in column A of " & strExcelFile
End If

Open in new window

0
 
RobSampsonCommented:
And with your code, it looks like strComputer in this bit
                        Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
                            strComputerName & "\root\cimv2:Win32_ComputerSystem.Name='" & _
                                strComputerName & "'")

was not defined, so I've changed that to strNewName so that it connects to the new name of the computer.

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"
 
' ** CONFIGURATION **
	Const strDomain = "domain"
	Const strAdminUser = "username"
	Const strAdminPass = "password"
	Const strLDAP_Server = "your.domain.com"
	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
' *******************
 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each objItem In colItems
      strSerial = objItem.SerialNumber
Next
 
Set objShell = CreateObject("WScript.Shell")
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
Set objSheet = objWB.Sheets(1)
strNewName = ""
For intRow = 2 To objSheet.Cells(65536, "A").End(xlUp).Row
	If UCase(objSheet.Cells(intRow, "A").Value) = UCase(strSerial) Then
		strNewName = UCase(objSheet.Cells(intRow, "B").Value)
		Exit For
	End If
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
 
Set objNetwork = CreateObject("WScript.Network")
strComputerName = objNetwork.ComputerName
 
If strNewName <> "" Then
	If UCase(strNewName) <> UCase(strComputerName) Then
		' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
		'WScript.Echo "Computer will now be renamed to " & strNewName
		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(strNewName))
			If intErrorCode <> 0 Then
				WScript.Echo "Error renaming computer. Error # " & intErrorCode
			Else
				'WScript.Echo "Computer renamed. It will now reboot."
				'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
				'objShell.Run "shutdown -r -t 00", 1, False
				
				WScript.Echo "Computer renamed. It will now attempt to join to the domain."
				 
				'strDomain = "Domain"
				'strAdminPass = "Password"
				'strAdminUser = "UserID"
				 				 
				Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
				    strNewName & "\root\cimv2:Win32_ComputerSystem.Name='" & _
				        strNewName & "'")
 
				ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
				    strAdminPass, strDomain & "\" & strAdminUser, NULL, _
				        JOIN_DOMAIN + ACCT_CREATE)
			End If
		Next
	Else
		WScript.Echo "Computer already has the correct name."
	End If
Else
	MsgBox "Could not find " & strSerial & " in column A of " & strExcelFile
End If

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
I will try this today.
0
 
RobSampsonCommented:
Hi, did you manage to test this code?

Regards,

Rob.
0
 
Dhiraj MuthaLevel DAuthor Commented:
I am really sorry, due to busy shcedule, i was not able to test it out. I will surely test it today and update you.
0
 
RobSampsonCommented:
Hi, did you manage to get around to this yet?  If not, don't stress....just checking....

Rob.
0
 
Dhiraj MuthaLevel DAuthor Commented:
No i tried both the scripts but no luck. It only changes the Hostname but does not add the system to reuired domain. Please have a check.
0
 
RobSampsonCommented:
I'm sorry. I'm not able to test it today.....if it *does* rename the computer, what happens after you reboot, then run the script again, while it already has the correct name?  Does it join the domain then?

Regards,

Rob.
0
 
Dhiraj MuthaLevel DAuthor Commented:
Once i run the script again, it does not do anything. And does not add the system to domain.
0
 
RobSampsonCommented:
OK, I've used thes script you provided, but the computer is unable to join the domain without rebooting after being renamed.

If you use this script, it will rename, attempt to join the domain, and then if that's not successful, it will tell you to reboot, and you can run the same script again to join the domain.

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"
 
' ** CONFIGURATION **
	Const strDomain = "MAROONDAHCC"
	Const strAdminUser = "Administrator"
	strAdminPass = InputBox("Enter your password:", "Password")
	Const strLDAP_Server = "maroondah.local"
	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
' *******************
 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each objItem In colItems
      strSerial = objItem.SerialNumber
Next
 
Set objShell = CreateObject("WScript.Shell")
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
Set objSheet = objWB.Sheets(1)
strNewName = ""
For intRow = 2 To objSheet.Cells(65536, "A").End(xlUp).Row
	If UCase(objSheet.Cells(intRow, "A").Value) = UCase(strSerial) Then
		strNewName = UCase(objSheet.Cells(intRow, "B").Value)
		Exit For
	End If
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
 
Set objNetwork = CreateObject("WScript.Network")
strComputerName = objNetwork.ComputerName
 
If strNewName <> "" Then
	If UCase(strNewName) <> UCase(strComputerName) Then
		' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
		'WScript.Echo "Computer will now be renamed to " & strNewName
		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(strNewName))
			If intErrorCode <> 0 Then
				WScript.Echo "Error renaming computer. Error # " & intErrorCode
			Else
				'WScript.Echo "Computer renamed. It will now reboot."
				'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
				'objShell.Run "shutdown -r -t 00", 1, False
	
				WScript.Echo "Computer renamed. It will now attempt to join to the domain."
	 			JoinDomain strNewName
			End If
		Next
	Else
		WScript.Echo "Computer already has the correct name."
		If PartOfDomain(".") = False Then
			MsgBox "This computer will now be joined to the domain."
			JoinDomain strNewName
			MsgBox "This computer has been joined to the domain."
		Else
			MsgBox "This computer is already part of a domain."
		End If
	End If
Else
	MsgBox "Could not find " & strSerial & " in column A of " & strExcelFile
End If
 
Sub JoinDomain(strComputer)
	'strDomain = "Domain"
	'strAdminPass = "Password"
	'strAdminUser = "UserID"
	 				 
	On Error Resume Next
	Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
	    strNewName & "\root\cimv2:Win32_ComputerSystem.Name='" & _
	        strComputer & "'")
	If Err.Number <> 0 Then
		MsgBox "Unable to join " & strComputer & " to the domain. Please reboot and try again."
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0
		ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
		    strAdminPass, strDomain & "\" & strAdminUser, NULL, _
		        JOIN_DOMAIN + ACCT_CREATE)
				
		'MsgBox "Return Value: " & ReturnValue
	End If
End Sub
 
Function PartOfDomain(strComputer)
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
	Set colComputers = objWMIService.ExecQuery ("Select PartOfDomain from Win32_ComputerSystem")
	For Each objComputer In colComputers
		strPartOfDomain = objComputer.PartOfDomain
	Next
	PartOfDomain = strPartOfDomain
End Function

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
I will be able to try this script on Monday.
0
 
Dhiraj MuthaLevel DAuthor Commented:
That was superb. The script worked fine. I will be ready to close the it off, once you provide me a changed script. I want the XL file to be accessed from the following path:

"\\indt01946\HP9050\Hostnames.xls"
0
 
RobSampsonCommented:
OK great. That's easy....just change this line
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"

to this
strExcelFile = "\\indt01946\HP9050\Hostnames.xls"

Regards,

Rob.
0
 
Dhiraj MuthaLevel DAuthor Commented:
Hi Rob, I am increasing the points to 150. If you can help me more as Script is working fine now. My query is:

1) Once we run the script, and if it is not able to add the system to domain, it should restart automatically and then run the script again AUTOMATICALLY.
2) Once the system is added to the domain, it should reset the password of local administrator account.

Can this be done?
0
 
Dhiraj MuthaLevel DAuthor Commented:
Actually i have increased it to 175.
0
 
RobSampsonCommented:
OK, so I think I've done point 1 here, but I haven't tested it.  What it now does is attempt to join the domain, then if that's unsuccessful the first time, it will add a registry value to HKLM\Software\Microsoft\Windows\CurrentVersion\Run to run the script immediately after logon after a reboot.  Then, when the script runs the second time, it will detect that the name is already correct, and should join the domain successfully.

For point two, I've added a ChangeLocalAdminPassword sub. The password you need to set is defined in that sub.

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"
 
' ** CONFIGURATION **
	Const strDomain = "MAROONDAHCC"
	Const strAdminUser = "Administrator"
	strAdminPass = InputBox("Enter your password:", "Password")
	Const strLDAP_Server = "maroondah.local"
	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
' *******************
 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each objItem In colItems
      strSerial = objItem.SerialNumber
Next
 
Set objShell = CreateObject("WScript.Shell")
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
Set objSheet = objWB.Sheets(1)
strNewName = ""
For intRow = 2 To objSheet.Cells(65536, "A").End(xlUp).Row
	If UCase(objSheet.Cells(intRow, "A").Value) = UCase(strSerial) Then
		strNewName = UCase(objSheet.Cells(intRow, "B").Value)
		Exit For
	End If
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
 
Set objNetwork = CreateObject("WScript.Network")
strComputerName = objNetwork.ComputerName
 
If strNewName <> "" Then
	If UCase(strNewName) <> UCase(strComputerName) Then
		' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
		'WScript.Echo "Computer will now be renamed to " & strNewName
		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(strNewName))
			If intErrorCode <> 0 Then
				WScript.Echo "Error renaming computer. Error # " & intErrorCode
			Else	
				WScript.Echo "Computer renamed. It will now attempt to join to the domain."
	 			intReturn = JoinDomain(strNewName)
	 			If intReturn <> 0 Then
					objShell.Popup "Joining to domain failed. This computer will now reboot and try again.", 3, "Computer Renamed"
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					strCommand = "cmd /c REG ADD HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""VBS Join Domain"" /d ""wscript.exe " & WScript.ScriptFullName & """ /f"
					objShell.Run strCommand, 1, True
					objShell.Run "shutdown -r -t 00", 1, False
				Else
					WScript.Echo "This computer has been joined to the domain."
					ChangeLocalAdminPassword strComputer
					WScript.Echo "Local admin password has been changed."
				End If
			End If
		Next
	Else
		WScript.Echo "Computer already has the correct name."
		If PartOfDomain(".") = False Then
			MsgBox "This computer will now be joined to the domain."
 			intReturn = JoinDomain(strNewName)
 			If intReturn <> 0 Then
				objShell.Popup "Joinig to domain failed. This computer will now reboot and try again.", 3, "Computer Renamed"
				'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
				objShell.Run "shutdown -r -t 00", 1, False
			Else
				WScript.Echo "This computer has been joined to the domain."
				ChangeLocalAdminPassword strComputer
				WScript.Echo "Local admin password has been changed."
			End If
		Else
			MsgBox "This computer is already part of a domain."
		End If
	End If
Else
	MsgBox "Could not find " & strSerial & " in column A of " & strExcelFile
End If
 
Function JoinDomain(strComputer)
	'strDomain = "Domain"
	'strAdminPass = "Password"
	'strAdminUser = "UserID"
	 				 
	On Error Resume Next
	Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
	    strNewName & "\root\cimv2:Win32_ComputerSystem.Name='" & _
	        strComputer & "'")
	If Err.Number <> 0 Then
		MsgBox "Unable to join " & strComputer & " to the domain. Please reboot and try again."
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0
		ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
		    strAdminPass, strDomain & "\" & strAdminUser, NULL, _
		        JOIN_DOMAIN + ACCT_CREATE)
				
		'MsgBox "Return Value: " & ReturnValue
	End If
	JoinDomain = ReturnValue
End Function
 
Function PartOfDomain(strComputer)
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
	Set colComputers = objWMIService.ExecQuery ("Select PartOfDomain from Win32_ComputerSystem")
	For Each objComputer In colComputers
		strPartOfDomain = objComputer.PartOfDomain
	Next
	PartOfDomain = strPartOfDomain
End Function
 
Sub ChangeLocalAdminPassword(strComputer)
	Set objUser = GetObject("WinNT://" & strComputer & "/Administrator") 
	objUser.SetPassword "x%tY7iu8%4f"
End Sub

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
I will surely try this today. And if working than we will close this out.
0
 
Dhiraj MuthaLevel DAuthor Commented:
After running that script, it renames the computer and it gives me the options attached back to back. And it does not restart the system as well.
DomainScript.doc
0
 
RobSampsonCommented:
OK, yeah, I had the logic a bit wrong there.  I've tested it, and as far as I can see, this works fine.

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Serials_Hostnames.xls"
 
' ** CONFIGURATION **
	Const strDomain = "YOURDOMAIN"
	Const strAdminUser = "Administrator"
	strAdminPass = InputBox("Enter your password:", "Password")
	Const strLDAP_Server = "domain.com"
	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
' *******************
 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT SerialNumber FROM Win32_BIOS")
For Each objItem In colItems
      strSerial = objItem.SerialNumber
Next
 
Set objShell = CreateObject("WScript.Shell")
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = False
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
Set objSheet = objWB.Sheets(1)
strNewName = ""
For intRow = 2 To objSheet.Cells(65536, "A").End(xlUp).Row
	If UCase(objSheet.Cells(intRow, "A").Value) = UCase(strSerial) Then
		strNewName = UCase(objSheet.Cells(intRow, "B").Value)
		Exit For
	End If
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
 
Set objNetwork = CreateObject("WScript.Network")
strComputerName = objNetwork.ComputerName
 
If strNewName <> "" Then
	If UCase(strNewName) <> UCase(strComputerName) Then
		' THIS SECTION WILL RENAME THE COMPUTER AND REBOOT
		'WScript.Echo "Computer will now be renamed to " & strNewName
		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(strNewName))
			If intErrorCode <> 0 Then
				WScript.Echo "Error renaming computer. Error # " & intErrorCode
			Else	
				WScript.Echo "Computer renamed. It will now attempt to join to the domain."
	 			intReturn = JoinDomain(strNewName)
	 			If intReturn <> 0 Then
					objShell.Popup "Joining to domain failed. This computer will now reboot and try again.", 3, "Computer Renamed"
					'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
					strCommand = "cmd /c REG ADD HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""VBS Join Domain"" /d ""wscript.exe " & WScript.ScriptFullName & """ /f"
					objShell.Run strCommand, 1, True
					objShell.Run "shutdown -r -t 00", 1, False
				Else
					WScript.Echo "This computer has been joined to the domain."
					strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""VBS Join Domain"" /f"
					objShell.Run strCommand, 0, True
					intReturn = ChangeLocalAdminPassword(strComputerName)
					If intReturn = 0 Then
						WScript.Echo "Local admin password has been changed."
					Else
						WScript.Echo "Local admin password was not changed."
					End If
				End If
			End If
		Next
	Else
		WScript.Echo "Computer already has the correct name."
		If PartOfDomain(".") = False Then
			MsgBox "This computer will now be joined to the domain."
 			intReturn = JoinDomain(strNewName)
 			If intReturn <> 0 Then
				objShell.Popup "Joining to domain failed. This computer will now reboot and try again.", 3, "Computer Renamed"
				'objShell.Run "rundll32 shell32.dll,SHExitWindowsEx 2", 1, False
				objShell.Run "shutdown -r -t 00", 1, False
			Else
				WScript.Echo "This computer has been joined to the domain."
				strCommand = "cmd /c REG DELETE HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""VBS Join Domain"" /f"
				objShell.Run strCommand, 0, True
				intReturn = ChangeLocalAdminPassword(strComputerName)
				If intReturn = 0 Then
					WScript.Echo "Local admin password has been changed."
				Else
					WScript.Echo "Local admin password was not changed."
				End If
			End If
		Else
			MsgBox "This computer is already part of a domain."
		End If
	End If
Else
	MsgBox "Could not find " & strSerial & " in column A of " & strExcelFile
End If
 
Function JoinDomain(strComputer)
	'strDomain = "Domain"
	'strAdminPass = "Password"
	'strAdminUser = "UserID"
	 				 
	On Error Resume Next
	Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
	    strNewName & "\root\cimv2:Win32_ComputerSystem.Name='" & _
	        strComputer & "'")
	If Err.Number <> 0 Then
		'MsgBox "Unable to join " & strComputer & " to the domain. Please reboot and try again."
		Err.Clear
		On Error GoTo 0
		ReturnValue = -1
	Else
		On Error GoTo 0
		ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
		    strAdminPass, strDomain & "\" & strAdminUser, NULL, _
		        JOIN_DOMAIN + ACCT_CREATE)
				
		'MsgBox "Return Value: " & ReturnValue
	End If
	JoinDomain = ReturnValue
End Function
 
Function PartOfDomain(strComputer)
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
	Set colComputers = objWMIService.ExecQuery ("Select PartOfDomain from Win32_ComputerSystem")
	For Each objComputer In colComputers
		strPartOfDomain = objComputer.PartOfDomain
	Next
	PartOfDomain = strPartOfDomain
End Function
 
Function ChangeLocalAdminPassword(strComputer)
	intError = 0
	If strComputer = "." Then
		Set objNetwork = CreateObject("WScript.Network")
		strComputer = objNetwork.ComputerName
	End If
	MsgBox "WinNT://" & strComputer & "/Administrator"
	On Error Resume Next
	Set objUser = GetObject("WinNT://" & strComputer & "/Administrator")
	objUser.SetPassword "x%tY7iu8%4f"
	If Err.Number <> 0 Then intError = -1
	Err.Clear
	On Error GoTo 0
	ChangeLocalAdminPassword = intError
End Function

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
Ok. I will test it out today. That was really quick.
0
 
RobSampsonCommented:
Cool. Hopefully it works.  If not, post a new question to get someone else to check it out.  I will not be available for the next two weeks.

Regards,

Rob.
0
 
Dhiraj MuthaLevel DAuthor Commented:
The script is running fine. But i am getting the following error (Attached).

What i see in Regedit:
wscript.exe C:\Documents and Settings\Administrator\Desktop\Domain12.vbs

What i Change:
wscript.exe "C:\Documents and Settings\Administrator\Desktop\Domain12.vbs"


If i make changes in registry it works fine in the startup and adds the system to domain. Please spare sometime and get this resolved.
0
 
Dhiraj MuthaLevel DAuthor Commented:
Can you help on this please?
0
 
Dhiraj MuthaLevel DAuthor Commented:
Thanks for all the help.
0
 
RobSampsonCommented:
Hi, I'm very sorry for my delay. I was away for longer than expected.  To resolve this issue, you should only need to change this line (line 61 above):
                              strCommand = "cmd /c REG ADD HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""VBS Join Domain"" /d ""wscript.exe " & WScript.ScriptFullName & """ /f"

to this
                              strCommand = "cmd /c REG ADD HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v ""VBS Join Domain"" /d ""wscript.exe \""" & WScript.ScriptFullName & "\"""" /f"


Regards,

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 27
  • 17
Tackle projects and never again get stuck behind a technical roadblock.
Join Now