Solved

Rename computer & join domain

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

708 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

12 Experts available now in Live!

Get 1:1 Help Now