Solved

Rename computer & join domain

Posted on 2006-10-20
4
1,192 Views
Last Modified: 2008-11-07
Microsoft has a 2 scripts script to automatically rename a computer and join it to a domain.  I'm wondering if these can be modified in 2 ways:

1. Prompt for the Computer Name instead of having it hard coded

2. If the computer account already esists on the domain, delete from the domain first then join to the domain.

Here are the 2 scripts provided by Microsoft:

Rename a Computer:

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

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

For Each objComputer in colComputers
    err = objComputer.Rename("WebServer")
Next



Join a Computer to a Domain:

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 = "FABRIKAM"
strPassword = "ls4k5ywA"
strUser = "shenalan"
 
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)
0
Comment
Question by:mgcIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 17781125
Hi mgcIT,

The answer to question #1 is to replace this line

    strComputer = "."

with

    strComputer = InputBox("Enter the name of the computer you want to join to the domain.", "Computer Name", ".")

I'll have to check on the second portion of the question.

Cheers!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17781244
mgcIT,

Here's the answer to #2.  This will enumerate the Computer container on the given AD server and check to see if it already contains a computer with the given name.


If InDomain("SomeComputerName") Then
    WScript.Echo "Already there"
Else
    WScript.Echo "Needs to be added"
End If

Function InDomain(strSystemName)
    Dim colComputers, objComputer
    InDomain = False
    'Fill in the information for your domain
    Set colComputers = GetObject("LDAP://server.company.com/cn=computers,dc=company,dc=com")
    colComputers.Filter = Array("Computer")
    For Each objComputer In colComputers
        If LCase(Trim(Mid(objComputer.Name, 4))) = LCase(strSystemName) Then
            InDomain = True
            Exit For
        End If
    Next
    Set objComputer = Nothing
    Set colComputers = Nothing
End Function
0
 

Expert Comment

by:cheshirefire
ID: 22902962
This is exactly what I want to do, prompt for the computername when the PC boots after reimaging and check of the account already exists.

However, I have no idea where or how to use this script?

Some help would be great

Thanks
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22903122
cheshirefire,

Here's how to use this.

1.  Open Notepad
2.  Copy the script and paste it into Notepad
3.  Edit the script as needed
4.  Save the file with a .vbs extension
5.  Double-click the file to run the script.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 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