Solved

Rename computer & join domain

Posted on 2006-10-20
4
1,181 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

914 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

18 Experts available now in Live!

Get 1:1 Help Now