INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...I wish I knew about this site years ago. It would have saved me a lot of heartaches..."

Geography

Where in the world do Tek-Tips members come from?

 Change hostname & join domain

acl03 (MIS)
9 Jun 06 9:23
Is it possible to change a computer name and join a domain under that new name all in 1 script?



Thanks,
Andrew

markdmac (MIS)
9 Jun 06 10:18
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 http://www.thespidersparlor.com/vbscript

acl03 (MIS)
9 Jun 06 11:34
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

markdmac (MIS)
9 Jun 06 11:43
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 http://www.thespidersparlor.com/vbscript

acl03 (MIS)
9 Jun 06 11:44
Great, I'll just do it that way.



Thanks,
Andrew

tsuji (TechnicalUser)
9 Jun 06 11:45

s="123.234.012.210"
set cm=regexp.execute(s)
wscript.echo cm(0).submatches(0) & vbcrlf & cm(0).submatches(1)

acl03 (MIS)
12 Jun 06 14:36
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

markdmac (MIS)
14 Jun 06 10:40
Only problem I see with the above script is it wasn't written to support automation by taking a list of computer names from a file.

I hope you find this post helpful.  

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

acl03 (MIS)
14 Jun 06 12:07
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

mrmovie (TechnicalUser)
14 Jun 06 14:18
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
markdmac (MIS)
14 Jun 06 15:31
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: http://www.thespidersparlor.com
' 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 http://www.thespidersparlor.com/vbscript

acl03 (MIS)
15 Jun 06 8:55
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

mrmovie (TechnicalUser)
15 Jun 06 10:11
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 smile
markdmac (MIS)
18 Jun 06 22:49
Look for a vbscript book by Ed Wilson (MS Press).  He works for Microsoft and created the internal course on vbscript.

I hope you find this post helpful.  

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

solec (IS/IT--Management)
28 Nov 06 5:14
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
Helpful Member!PHV (MIS)
28 Nov 06 5:39
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

solec (IS/IT--Management)
28 Nov 06 5:44
Hi ,

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

Solec
Helpful Member!PHV (MIS)
28 Nov 06 6:03
"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

Start A New Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Promoting, selling, recruiting and student posting
are not allowed in the forums.
Posting Policies

LINK TO THIS FORUM!
(Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum)
TITLE: VBScript Forum at Tek-Tips
URL: http://www.tek-tips.com/threadminder.cfm?pid=329
DESCRIPTION: VBScript technical support forum and mutual help system for computer professionals. Selling and recruiting forbidden.

 

Back To Forum