Solved

Rename computer & join domain

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Paint/Redraw window while dragging 16 85
Access Object Property from VBA Module in Excel 2010 2 39
Hide vba in gp 7 113
Excel Automation VBA 19 101
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

679 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