• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1305
  • Last Modified:

VBScript: WMI error joining the domain

I have built a WIndows XP image that we are deploying in our office.  Part of the image uses the JoinDomainOrWorkGroup object to add the machine to the domain but the script periodically fails on the line:

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
                  strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
                        strComputer & "'")

It seems if I reboot the machine a couple of times it will complete the script.  

Details:

I am using the code that MS recommends for this task.
http://msdn.microsoft.com/en-us/library/aa392154(VS.85).aspx

The error it throws is:
C:\...\File.vbs (line,3) (null): 0x80041002

When I output the Err.Number to the screen, the error is: 2147217406

I have tried removing the machine from the domain 15 minutes or more before imaging the machine and running the script.  No change.

I have tried adding a reboot on the first boot after Sysprep.  No Change.

Prior to running this portion of the script, the local computer name and description are changed.

In some instances, I have run the script, had it fail, then run it again a few minutes later and had it complete.

I have also tried setting up a loop in which in the case of errors, validates whether or not the computer has completed joining the domain, and if not, loops back to the start of the Sub.

I'm going to start working on a workaround, but this is my preferred method.  I would rather not use Netdom because I don't want to store account credentials in the script.



Ideas?  Can someone at least tell me what is happening at that line?
Sub DomainJoin(strUser, strPassword)

	' Declare Sub objects and variables
	Dim objNetwork, objComputer
	Dim strComputer, strDomain, strOrgUnit, strReturnValue, strRetry
	Const JOIN_DOMAIN = 1 
	Const ACCT_CREATE = 2 
	
	strDomain = "DOMAIN.COM"
	strOrgUnit = "OU=" & GetSystemVariable("Dept") & ";OU=OU;DC=DC;DC=COM"
	
	' Lookup the name of the computer
	Set objNetwork = CreateObject("WScript.Network") 
	strComputer = objNetwork.ComputerName 
	
	' Pause the script for 10 seconds before attempting to join the domain
	Wscript.Sleep(10000) 
	
	Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _ 
			strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _ 
				strComputer & "'") 
		 
	' Join the domain
	strReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, _
			strDomain & "\" & strUser, strOrgUnit, JOIN_DOMAIN + ACCT_CREATE) 
											

End Sub

Open in new window

0
RunningGag
Asked:
RunningGag
  • 3
  • 2
3 Solutions
 
pschakravarthiCommented:
Hi,

It might be because of WMI database issues.
You can rebuild it through following steps

These errors will occur if the %SystemRoot%\System32\Wbem\Repository folder is
damaged.
To fix this problem:
1. Use the Start menu to right-click My Computer.
2. Press the Manage item.
3. Double-click Services and Applications in the left-hand pane.
4. Press Services to expand it.
5. Scroll to Windows Management Instrumentation in the right-hand pane and
right-click it.
6. Press Stop.
7. Use Windows Explorer to delete all the files in the
%SystemRoot%\System32\Wbem\Repository folder.
8. Shutdown and restart your computer. The Windows Management Instrumentation
service will start and the files will be re-created.
0
 
RunningGagAuthor Commented:
I will give it a shot, but this is a brand new installation of Windows XP.  The only thing I've done is update Windows, and run it through Sysprep a couple of times.

Would you expect this to happen intermittently if the repository is damaged?
0
 
pschakravarthiCommented:
Yes.  Most of the cases this would be because of repository damage.

You can do wbemtest on that machine.
Start->Run->wbemtest
Then click on connect
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
RunningGagAuthor Commented:
Okay.  Reimaged the machine and tried your first recommendation prior to executing the scripts.  No change.

I'm not sure what you would like me to do with the wbemtest tool.
0
 
merowingerCommented:
we assume that there could might be a problem with the WMI Object.
the wbemtest tool can test the wmi connection.
Could you please run the following script instead of yours to get the error information when connecting to WMI and joining computer to domain.
Sub DomainJoin(strUser, strPassword) 
 
        ' Declare Sub objects and variables 
        Dim objNetwork, objComputer 
        Dim strComputer, strDomain, strOrgUnit, strReturnValue, strRetry 
        Const JOIN_DOMAIN = 1  
        Const ACCT_CREATE = 2  
         
        strDomain = "DOMAIN.COM" 
        strOrgUnit = "OU=" & GetSystemVariable("Dept") & ";OU=OU;DC=DC;DC=COM" 
         
        ' Lookup the name of the computer 
        Set objNetwork = CreateObject("WScript.Network")  
        strComputer = objNetwork.ComputerName  
         
        ' Pause the script for 10 seconds before attempting to join the domain 
        Wscript.Sleep(10000)  
        
	On Error Resume Next
        Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _  
                        strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _  
                                strComputer & "'") 
	WScript.Echo "Error: " & Err.Number
	WScript.Echo "Error (Hex): " & Hex(Err.Number)
	WScript.Echo "Source: " &  Err.Source
	WScript.Echo "Description: " &  Err.Description
	Err.Clear
                  
        ' Join the domain
	On Error Resume Next
        strReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, _ 
                        strDomain & "\" & strUser, strOrgUnit, JOIN_DOMAIN + ACCT_CREATE)  
	WScript.Echo "Error: " & Err.Number
	WScript.Echo "Error (Hex): " & Hex(Err.Number)
	WScript.Echo "Source: " &  Err.Source
	WScript.Echo "Description: " &  Err.Description
	Err.Clear                                                                                         
 
End Sub

Open in new window

0
 
RunningGagAuthor Commented:
Hi folks, sorry for the delay getting back to you.  I've determined that it is just that when using VBScript, the machine is demanding a reboot between changing the name and joining the domain.  Since this image isn't being deployed in bulk, I can deal with the extra reboot.

For future reference, if a person wants to avoid the reboot, we have found that using Netdom to join the domain will allow you to bypass the additional reboot.  Just make sure you add error checking to verify that it joined because sometimes it fails.

Thanks for the suggestions.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now