Solved

Rename computer & join domain

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 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