AllExperts > Experts 
Search      

VB Script

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB Script Answers
Question Library

Ask a question about VB Script
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Igor
Expertise
WSH 5.6, ADO, CDO, WMI, IE as Object.

Experience
WSH 5.6, ADO, CDO, WMI, IE as Object.

 
   

You are here:  Experts > Computing/Technology > Basic > VB Script > Change computer name and add to the domain

Topic: VB Script



Expert: Igor
Date: 5/23/2005
Subject: Change computer name and add to the domain

Question
Hello,

I've been working on a script that will help me change a computer's name and then add that computer to a domain.  As I have it set up now the script prompts me for a Computer name and a Domain name to add the computer to.  The script works fine on a test computer at home (except it adds it to a workgroup and not a domain)

However when testing this in the network environment at work, it fails to rename the computer or add it to  a domain.  I have it setup to return errors and it returns Error # 0.

I am pasting the script below.  Can you tell me what I might be doing wrong ?  Or if there is a better way to go about creating a script to change a computer's name and join it to a domain?  I want to use this script so I can perform this task on multiple computers.   Any assistance you can provide is greatly appreciated.

Thanks,  SCRIPT IS BELOW
-----------------------

Name = InputBox("Enter the new computer name below and click OK to continue","Rename: Step 1")

Password = "techlab"
Username = "wgmanager"

Set objWMIService = GetObject("Winmgmts:root\cimv2")

' Call always gets only one Win32_ComputerSystem object.
For Each objComputer in _
   objWMIService.InstancesOf("Win32_ComputerSystem")

       Return = objComputer.rename(Name,Password,User)
       If Return <> 0 Then
          WScript.Echo "Rename failed. Error = " & Err.Number
       Else
          WScript.Echo "Rename succeeded." & " Reboot for new name to go into effect"
       End If



Next



DomainName = InputBox("Enter the Domain to join this computer to and click OK to continue","Rename: Step 1")

DomainPassword = "password"
DomainUsername = "username"

Set objWMIService = GetObject("Winmgmts:root\cimv2")

' Call always gets only one Win32_ComputerSystem object.
For Each objComputer in _
   objWMIService.InstancesOf("Win32_ComputerSystem")

       Return = objComputer.JoinDomainOrWorkgroup(DomainName,DomainPassword,DomainUsername)
       If Return <> 0 Then
          WScript.Echo "Rename failed. Error = " & Err.Number
       Else
          WScript.Echo "Rename succeeded." & " Reboot for new name to go into effect"
       End If



Next  

Answer
Joins the local computer to a domain and creates the computer's account in Active Directory.

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)




The JoinDomainOrWorkgroup method joins a computer system to a domain or workgroup. This method is new for Windows® XP.!

Function JoinDomainOrWorkgroup( _
 ByVal Name As String, _
 ByVal Password As String, _
 ByVal UserName As String, _
 [ ByVal AccountOU As String ], _
 ByVal FJoinOptions As Integer _
) As Integer
Parameters
Name
[in] Specifies the domain or workgroup to join. Cannot be NULL.
Password
[in] If the UserName parameter specifies an account name, the Password parameter must point to the password to use when connecting to the domain controller. Otherwise, this parameter must be NULL.
Password must use a high authentication level, not less than RPC_C_AUTHN_LEVEL_PKT_PRIVACY when connecting to Winmgmt or SetProxyBlanket on the IWbemServices pointer. If local to Winmgmt this is not a concern.

UserName
[in] Pointer to a constant null-terminated character string that specifies the account name to use when connecting to the domain controller. Must specify a domain NetBIOS name and user account, for example, Domain\user. If this parameter is NULL, the caller's context is used.
Windows XP, Windows .NET Server 2003 family:  Using the user principal name (UPN) in the form user@domain is also supported.

UserName must use a high authentication level, not less than RPC_C_AUTHN_LEVEL_PKT_PRIVACY when connecting to Winmgmt or SetProxyBlanket on the IWbemServices pointer. If local to Winmgmt this is not a concern.

AccountOU
[in, optional] Specifies the pointer to a constant null-terminated character string that contains the RFC 1779 format name of the organizational unit (OU) for the computer account. If you specify this parameter, the string must contain a full path, otherwise AccountOU must be NULL.
Example: OU=testOU, DC=domain, DC=Domain, DC=com
FJoinOptions
[in] Set of bit flags defining the join options.

Value Meaning
0 Join Domain
Default. Joins the computer to a domain. If this value is not specified, joins the computer to a workgroup.

1 Acct Create
Creates the account on the domain.

2 Acct Delete
Delete the account when the domain is left.

4 Win9X Upgrade
The join operation is part of an upgrade of Windows 95/98 to Windows NT/2000.

5 Domain Join If Joined
Allows a join to a new domain even if the computer is already joined to a domain.

6 Join Unsecure
Performs an unsecured join.

7 Machine Password Passed
The machine, not the user, password passed. This option is only valid for unsecure joins.

8 Deferred SPN Set
Writing SPN and DnsHostName attribtes on the computer object should be deferred until the rename that follows the join.

18 Install Invocation
The APIs were invoked during install.


Return Values
The JoinDomainOrWorkgroup method returns 0 on success or if no options are involved. Any other value indicates an error.


Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.