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")
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.
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.