Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change hostname & join domain 1

Status
Not open for further replies.

acl03

MIS
Jun 13, 2005
1,077
0
0
US
Is it possible to change a computer name and join a domain under that new name all in 1 script?



Thanks,
Andrew
 
Yes, you will want to use the Netdom resource kit utility and use vbscript to automate that.

If the machine is already a member of the domain then you can use netdom to just rename it without having to rejoin.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Thanks Mark.

One more question - should be simple. I've never done regular expressions in VBS before, just perl.

Say I want to pull out the last 2 nodes of an IP address to 2 separate variables:
Code:
Set regexp = New RegExp
regexp.Pattern = "\d+\.\d+\.(\d+)\.(\d+)"

How do I pull out the last two sets of digits that I have surrounded by parenthesis? In perl they are referenced $1 and $2. Any idea?



Thanks,
Andrew
 
Sorry I don't use reg expressions enough to answer you properly. I would do it with a split command myself.

Maybe not as elegant but it works.
Code:
ip="192.168.0.1"
iparray = Split(ip,".")
last2 = iparray(2) & "." & iparray(3)
WScript.Echo last2

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Great, I'll just do it that way.



Thanks,
Andrew
 
[tt]
s="123.234.012.210"
set cm=regexp.execute(s)
wscript.echo cm(0).submatches(0) & vbcrlf & cm(0).submatches(1)
[/tt]
 
This code i wrote/borrowed adds a computer to a domain, and changes the computer name with no reboot in between. It does it without using NETDOM. It also can add it in the OU of your choosing.

Code:
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
Dim message
Dim IP


strDomain = <DOMAIN NAME>
strUser = <UserName>
strPassword = <ADMIN PASSWORD>
 
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
 
Set objWMIService = GetObject ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

title = "Join computer to domain"
message = "Please enter computer name. Leave blank or press cancel to quit. & vbCrLf & vbCrLf & "Generated name: " & generatedName
newComputerName = InputBox(message, title)


If newComputerName = "" Then
	Wscript.quit
End If

areYousure = MsgBox("Are you sure you want t0 add computer to domain with name:" & vbCrLf & vbCrLf & newComputerName, vbYesNo + vbQuestion,"Add computer to domain")

If areYouSure = "7" Then
	MsgBox "Exiting script.",vbInformation
	Wscript.quit
End If 

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
        strComputer & "'")
 
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, "ou=My_Computer_OU,DC=mycorp,dc=com, _
        JOIN_DOMAIN + ACCT_CREATE)
        
If ReturnValue = 0 Then
	MsgBox "Computer added to domain under old name without error. proceeding to change computer name. "
Else
	MsgBox "Computer not added to domain successfully. Return value: " & ReturnValue
End If

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")


For Each objComputer in colComputers
	MsgBox "About to rename computer to: " & newComputername
    	ErrCode = objComputer.Rename(newComputerName, strPassword, strUser)
	If ErrCode = 0 Then
		MsgBox "Computer renamed correctly."
	Else
		MsgBox "Eror changing computer name. Error code: " & ErrCode
	End If

Next



Thanks,
Andrew
 
Well we'd be running this on a machine to add it to AD - how do you mean? How would taking in a name of files help?



Thanks,
Andrew
 
where you ask for the new computer name via an input box, newComputerName.

btw, you dont need to worry about getting strComputerName from WshNetwork...just use a "." in the WMI moniker strings

btw, you can add an OU property to the add2domain method, might allow you to override computers OU redirect
 
acl03 what I am talking about is true automation. Rather than running this script and having to type a name for each machine or having to run the script locally on each machine, it would be better to provide a list (perhaps in Excel) of machine names with what new name to join to the domain with.

If you had 1000 machines to join to the domain, the script you posted would be extremely tedious where a script designed to read data from a file would be considerably more useful.

As an example, here is a script I wrote that uses Netdom. I use this to move computers from one domain to another. I feed it a list of machine names and away it goes. I have used this script to move machines in a domain in Mexico to a parent domain in Arizona when the child domain was being dismantled.

Code:
'==========================================================================
'
' NAME: NetDomJoinWorkstations
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 6/24/2003
'
' COMMENT: Joins computers to a new domain.  Edit domain name, 
' user ID and passwords below.  Uses a workstation list wslist.txt. 
' Modification 7/28/2003 to include Remove command.  Suggest synchronizing old and new server passwords 
'
'==========================================================================

On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'open the data file
Set oTextStream = oFSO.OpenTextFile("wslist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

For Each strWorkstation In RemotePC
'Do something useful with strWorkstation
Call WSHShell.Run("cmd.exe /c NETDOM REMOVE " & strWorkstation &"/Domain:<domain> /UserD:<user> /PasswordD:<password> UserO:<user> /PasswordO:<password> /REBoot:30000")
Wscript.sleep 15000
Call WSHShell.Run("cmd.exe /c NETDOM JOIN " & strWorkstation &"/Domain:<domain> /UserD:<user> /PasswordD:<password> UserO:<user> /PasswordO:<password> /REBoot:0")

Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Mark, MrMovie,

Do you guys know a good book that would teach me how to do windows scripting? I'm kind of doing this by the seat of my pants. 90% of my learning has been from tek-tips (which is great), but i wonder if i should get myself a more solid foundation.



Thanks,
Andrew
 
i rate newriders Windows Management Intrusmentation WMI and 'ADSI scripting for sys admins' but i think the last one is out of print infact i think they might have folded..

the o'reily AD cookbook is a must, but there is a second edition coming out which is worth the wait.

i started with 'teach yourself vbscript in 24 days' and using the skills of PHV and tsuji on this site (for which i am grateful) and look at me know i am a legend :)
 
Hi,

I'm getting error on line 25 on this script expected end of statement, is this correct? Thanks,

Solec

Code:
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
Dim message
Dim IP


strDomain = <DOMAIN NAME>
strUser = <UserName>
strPassword = <ADMIN PASSWORD>
 
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
 
Set objWMIService = GetObject ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

title = "Join computer to domain"
message = "Please enter computer name. Leave blank or press cancel to quit. & vbCrLf & vbCrLf & "Generated name: " & generatedName
newComputerName = InputBox(message, title)


If newComputerName = "" Then
    Wscript.quit
End If

areYousure = MsgBox("Are you sure you want t0 add computer to domain with name:" & vbCrLf & vbCrLf & newComputerName, vbYesNo + vbQuestion,"Add computer to domain")

If areYouSure = "7" Then
    MsgBox "Exiting script.",vbInformation
    Wscript.quit
End If 

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
        strComputer & "'")
 
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, "ou=My_Computer_OU,DC=mycorp,dc=com, _
        JOIN_DOMAIN + ACCT_CREATE)
        
If ReturnValue = 0 Then
    MsgBox "Computer added to domain under old name without error. proceeding to change computer name. "
Else
    MsgBox "Computer not added to domain successfully. Return value: " & ReturnValue
End If

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")


For Each objComputer in colComputers
    MsgBox "About to rename computer to: " & newComputername
        ErrCode = objComputer.Rename(newComputerName, strPassword, strUser)
    If ErrCode = 0 Then
        MsgBox "Computer renamed correctly."
    Else
        MsgBox "Eror changing computer name. Error code: " & ErrCode
    End If

Next
 
message = "Please enter computer name. Leave blank or press cancel to quit.[!]"[/!] & vbCrLf & vbCrLf & "Generated name: " & generatedName

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi ,

Im now getting error on line 44 , unterminated string constant, Please help. Thanks

Solec
 
ou=My_Computer_OU,DC=mycorp,dc=com[!]"[/!], _

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top