Join W2K Domain

Hi,

how can i join a Workstation to a W2K3 Domain with a C# Prog
Please post a code sample.

Andi


Answer this question

Join W2K Domain

  • pellarin

    I was looking for the same code. Since implementing it was so easy, I loaded up my Win2k VMWare machine and ran it against my Win2k3 Server VMWare machine. It does NOT work on Windows 2000. The error returned is a "ManagementException" with this message: "This method is not implemented in any class."

    Just thought I'd share this since you guys definitely helped me out.

    Thanks,
    Chris


  • General Gigilo

    One way would be to use an instance of the WMI class Win32_ComputerSystem and the JoinDomainOrWorkgroup method of that class.

    The method itself is described here
    http://msdn.microsoft.com/library/en-us/wmisdk/wmi/joindomainorworkgroup_method_in_class_win32_computersystem.asp

    To invoke the method, first get the right instance ofWin32_ComputerSystem
    http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemManagementManagementObjectClassctorTopic3.asp

    The sample at that page creates an instance of Win32_Service for the name Alerter.  Change that to Win32_ComputerSystem and use the name of your computer system.

    Once you have the instance, use the InvokeMethod method to invoke JoinDomainOrWorkgroup and pass an object array holding the parameters for that method.

    The following code works for me on my laptop joining a test domain, your mileage may vary.  The last parameter could be a '1' instead of a '3' if the computer account already exists in the domain you are joining but a 3 works in both cases.


    ManagementObject o = 
       new ManagementObject("Win32_ComputerSystem.Name='Host1'");

    object[] methodArgs = {"DomainName", "Password",
       @"DomainName\UserName", null, 3};

    object result = o.InvokeMethod ("JoinDomainOrWorkgroup", methodArgs);

     

    Note:  The code above changes the configured Domain Name, but the system needs to be restarted before the change will take effect.  The Shutdown or Reboot methods of Win32_OperatingSystem could be used to force this restart.


  • Michiel de Bruijn

    My example was incorrect (I said I hadn't tested it!)  The user name parameter must include a domain otherwise it uses the local Administrator account (hence the password error).

    A second problem with the example is that it would not create a computer account in the domain, so if the account wasn't created manually the function failed with error 1332.  Passing a '3' rather than a '1' as the last parameter avoids that problem by creating an account if needed.

    I've updated the original post to reflect these changes.


  • Navaron

    I have a similar problem to solve. Besides JoinDomainOrWorkgroup I also found NetJoinDomain.What is the main difference between those two functions
    If I got that right, then NetJoinDomain supports also Win2k in contrast to JoinDomainOrWorkgroup.
    Isn't NetJoinDomain then the better option to choose Or is there a big disadvantage I've missed

  • foleyp

    Thank you very much for that fine explanation!

    A few notes from my side: I've already implemented NetJoinDomain and thought that it's done with that. Unfortunately there were some problems reported where this feature failed to work. Instead of joining to the Domain it Joined to a Workgroup. I have absolutely no idea why that happened. Even if it only occurs on 5-10% of all users it's not working well. So I searched for an alternative and found JoinDomainOrWorkgroup but which pushed me off, because of the missing W2k support.

    But your conclusion makes sense. I'd like to implement JoinDomainOrWorkgroup and try it out myself. Unfortunately I don't have the possibility to access a W2k system right now. Looks, like I have to wait some time for this.

    Maybe someone could post his experience with JoinDomainOrWorkgroup (with W2k). That would help.

  • linnOz

    Has anybody got this code to work. I get an error # 1323 ie. unable to update the password. The value provided as the current password is incorrect.

    Anybody has any ideas

    Any help appreciated.

    thank you.


  • DaPoussin

    IJay wrote:
    Besides JoinDomainOrWorkgroup I also found NetJoinDomain.What is the main difference between those two functions

    The main difference is that JoinDomainOrWorkingGroup is a function implemented by the WMI class Win32_ComputerSystem whereas NetJoinDomain is an API function implemented within netapi32.dll and accessed via lm.h. In situations where WMI works but calling a C function is impossible or difficult (such as in VBScript for example) JoinDomainOrWorkingGroup will work and NetJoinDomain won't (or rather it can't be called).

    If you are coding in C/C++ or some other language that can easily invoke API functions (C# using P/Invoke for example) then NetJoinDomain may be easier to call. On the other hand, if you happen to be doing a bunch of WMI stuff anyway and so already have a suitable Win32_ComputerSystem instance available, calling JoinDomainOrWorkingGroup may be easier.

    If I got that right, then NetJoinDomain supports also Win2k in contrast to JoinDomainOrWorkgroup.
    Isn't NetJoinDomain then the better option to choose Or is there a big disadvantage I've missed

    Unfortunately the "Requirements" section of the documentation tends not to be rigorously written. Some authors choose to only list supported operating systems even though the feature was supported on earlier (now unsupported) operating systems. Some authors choose to list the complet set of operating systems the feature works on even though some of those operating systems are no longer supported.

    Just to make things harder, sometimes things really are only supported on newer operating systems so when (for example) a Client section only lists XP and Vista it is sometimes hard to tell if that is because the feature only works on XP and Vista or because the author only lists the supported subset of the operating systems the feature works on.

    I don't have time to go try this but I suspect that JoinDomainOrWorkingGroup also works on Windows 2000. Partly that's an educated guess based on how WMI works and partly it's an educated guess based on the note in the documentation for JoinDomainOrWorkingGroup's UserName parameter. The note says "Windows 2000, Windows NT 4.0, and Windows Me/98/95: You cannot specify UserName in UPPED format.". If JoinDomainOrWorkingGroup didn't work on Windows NT et al there wouldn't be much need for the note.


  • JeffreyCollins

    Thank you that worked just fine.
  • Join W2K Domain