Dhiraj Mutha
asked on
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.
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.
ASKER
My excel file is in network path. Will this code work with it.
ASKER
Also let me know what should be the format of the file i.e. how the columns should be named and all.
Hi, yes, you can change this:
strExcelFile = Replace(WScript.ScriptFull Name, WScript.ScriptName, "") & "Serials_Hostnames.xls"
to this
strExcelFile = "\\server\share\Serials_Ho stnames.xl s"
or
strExcelFile = "F:\Excel\Serials_Hostname s.xls"
and the format of the spreadsheet just has headers in Row 1 of
SERIAL NUMBER | NEW PC NAME
111222 | FINANCEPC1
Regards,
Rob.
strExcelFile = Replace(WScript.ScriptFull
to this
strExcelFile = "\\server\share\Serials_Ho
or
strExcelFile = "F:\Excel\Serials_Hostname
and the format of the spreadsheet just has headers in Row 1 of
SERIAL NUMBER | NEW PC NAME
111222 | FINANCEPC1
Regards,
Rob.
ASKER
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.
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.
Regards,
Rob.
ASKER
K. Will have a check today and get back on this.
ASKER
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.
2) Next is to add the system to domain and restart with the same script.
ASKER
The script is given me an error:
Line: 40
Char: 5
Error: Object required 'objshell'
Code: 800A01A8
Source: microsoft VBScript runtime error
ASKER
Resolved the error: just add the following line.
Set objShell = CreateObject("Wscript.Shel l")
Set objShell = CreateObject("Wscript.Shel
Hi. I'm not sure if the computer can join the domain before a reboot, but give this a shot.
Regards,
Rob.
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
ASKER
I will sure try this on tuesday, as i am travelling out of station. Thansk for the quick help.
ASKER
I m back now. Will have a check and update tomorrow.
ASKER
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
ASKER
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.Netw ork")
'strComputer = objNetwork.ComputerName
Set objComputer = GetObject("winmgmts:{imper sonationLe vel=Impers onate}!\\" & _
strComputer & "\root\cimv2:Win32_Compute rSystem.Na me='" & _
strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWo rkGroup(st rDomain, _
strPassword, strDomain & "\" & strUser, NULL, _
JOIN_DOMAIN + ACCT_CREATE)
Add-to-Domain.txt
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.Netw
'strComputer = objNetwork.ComputerName
Set objComputer = GetObject("winmgmts:{imper
strComputer & "\root\cimv2:Win32_Compute
strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWo
strPassword, strDomain & "\" & strUser, NULL, _
JOIN_DOMAIN + ACCT_CREATE)
Add-to-Domain.txt
Hi, you can either try my code again, but put this:
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
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.
Set objFSO = CreateObject("Scripting.Fi
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
ASKER
I will try it in sometime.
No problem....try either one, or both.....let me know....
Rob.
Rob.
ASKER
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)
2nd Script - Mine - It Renams the computer but throws the following error:
Line: 68
Char: 5
Error: 0x80041021
Code: 80041021
Source: (null)
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.
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
And with your code, it looks like strComputer in this bit
Set objComputer = GetObject("winmgmts:{imper sonationLe vel=Impers onate}!\\" & _
strComputerName & "\root\cimv2:Win32_Compute rSystem.Na me='" & _
strComputerName & "'")
was not defined, so I've changed that to strNewName so that it connects to the new name of the computer.
Regards,
Rob.
Set objComputer = GetObject("winmgmts:{imper
strComputerName & "\root\cimv2:Win32_Compute
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
ASKER
I will try this today.
Hi, did you manage to test this code?
Regards,
Rob.
Regards,
Rob.
ASKER
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.
Hi, did you manage to get around to this yet? If not, don't stress....just checking....
Rob.
Rob.
ASKER
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.
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.
Regards,
Rob.
ASKER
Once i run the script again, it does not do anything. And does not add the system to domain.
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.
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
ASKER
I will be able to try this script on Monday.
ASKER
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\Hostna mes.xls"
"\\indt01946\HP9050\Hostna
OK great. That's easy....just change this line
strExcelFile = Replace(WScript.ScriptFull Name, WScript.ScriptName, "") & "Serials_Hostnames.xls"
to this
strExcelFile = "\\indt01946\HP9050\Hostna mes.xls"
Regards,
Rob.
strExcelFile = Replace(WScript.ScriptFull
to this
strExcelFile = "\\indt01946\HP9050\Hostna
Regards,
Rob.
ASKER
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?
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?
ASKER
Actually i have increased it to 175.
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\Wi ndows\Curr entVersion \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.
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
ASKER
I will surely try this today. And if working than we will close this out.
ASKER
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
DomainScript.doc
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.
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
ASKER
Ok. I will test it out today. That was really quick.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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\Des ktop\Domai n12.vbs
What i Change:
wscript.exe "C:\Documents and Settings\Administrator\Des ktop\Domai n12.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.
What i see in Regedit:
wscript.exe C:\Documents and Settings\Administrator\Des
What i Change:
wscript.exe "C:\Documents and Settings\Administrator\Des
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.
ASKER
Can you help on this please?
ASKER
Thanks for all the help.
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\Wi ndows\Curr entVersion \Run /v ""VBS Join Domain"" /d ""wscript.exe " & WScript.ScriptFullName & """ /f"
to this
strCommand = "cmd /c REG ADD HKLM\Software\Microsoft\Wi ndows\Curr entVersion \Run /v ""VBS Join Domain"" /d ""wscript.exe \""" & WScript.ScriptFullName & "\"""" /f"
Regards,
Rob.
strCommand = "cmd /c REG ADD HKLM\Software\Microsoft\Wi
to this
strCommand = "cmd /c REG ADD HKLM\Software\Microsoft\Wi
Regards,
Rob.
Regards,
Rob.
Open in new window