Solved

Adding the system to Domain with a new Hostname

Posted on 2008-10-08
44
582 Views
Last Modified: 2012-05-05
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
Comment
Question by:Dhiraj Mutha
  • 27
  • 17
44 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 22674014
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22675568
My excel file is in network path. Will this code work with it.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22675599
Also let me know what should be the format of the file i.e. how the columns should be named and all.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22675821
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22675924
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22675957
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22676044
K. Will have a check today and get back on this.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22676761
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22676769


The script is given me an error:

Line: 40
Char: 5
Error: Object required 'objshell'
Code: 800A01A8
Source: microsoft VBScript runtime error
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22676824
Resolved the error: just add the following line.
Set objShell = CreateObject("Wscript.Shell")
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22682787
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22692949
I will sure try this on tuesday, as i am travelling out of station. Thansk for the quick help.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22719427
I m back now. Will have a check and update tomorrow.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22719685

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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22719783
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22725420
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22728121
I will try it in sometime.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22728131
No problem....try either one, or both.....let me know....

Rob.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22728603
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22735340
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22735374
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22737709
I will try this today.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 65

Expert Comment

by:RobSampson
ID: 22761713
Hi, did you manage to test this code?

Regards,

Rob.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22763849
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22819270
Hi, did you manage to get around to this yet?  If not, don't stress....just checking....

Rob.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22828482
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22837747
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22838623
Once i run the script again, it does not do anything. And does not add the system to domain.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22847021
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22857732
I will be able to try this script on Monday.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22920503
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22926071
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22928219
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22936792
Actually i have increased it to 175.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22945062
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22946866
I will surely try this today. And if working than we will close this out.
0
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22948312
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22956435
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22957136
Ok. I will test it out today. That was really quick.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 175 total points
ID: 22957174
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22957799
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
 
LVL 14

Author Comment

by:Dhiraj Mutha
ID: 22965921
Can you help on this please?
0
 
LVL 14

Author Closing Comment

by:Dhiraj Mutha
ID: 31504135
Thanks for all the help.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23126627
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now