none
I need to Convert code from VBScript to C# RRS feed

  • Question

  • Hi

    I have this code in VBScript use the WMI method to create new windows user Account and add it to administrators group... and I wish to convert this code to C#

    strComputer = "my-pc"
    Set colAccounts = GetObject("WinNT://" & strComputer & "")
    Set objUser = colAccounts.Create("user", "admin1")
    objUser.SetPassword "123456"
    objUser.SetInfo

    Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")

    Set objUser = GetObject("WinNT://" & strComputer & "/admin1,user")
    objGroup.Add(objUser.ADsPath)

    so what is the Equivalent C# code for that code ??

    and thanks with my best regarding
    Fraas

    Tuesday, September 5, 2006 9:23 PM

Answers

  • try this:

    add a reference to System.DirectoryServices. Then add this code:



    using System.DirectoryServices;
    ...
    ...
    private bool DoCreateUserAccount(string username, string password, string description)
    {
       try
       {
     
          DirectoryEntry theHost = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
          DirectoryEntry theUserAccount = theHost.Children.Add(username, "user");
     
          theUserAccount.Invoke("SetPassword", new object[] { password } );
          theUserAccount.Invoke("Put", new object[] { "Description", description } );
          theUserAccount.CommitChanges();
     
          return true;
     
       }
       catch (DirectoryServicesCOMException ex)
       {
          //handle the error
          return false;
       }
    }

     

     

    To add the user account into a specific group, add the following:

     



       DirectoryEntry theGroup;
       theGroup = theHost.Children.Find(GroupName, "group");
       if (theGroup != null)
       {
          theGroup.Invoke("Add", new object[] { theUserAccount.Path.ToString() } );
       }

     

    Tuesday, September 5, 2006 9:38 PM
    Moderator

All replies

  • ill work on it and get back to you hopefully
    Tuesday, September 5, 2006 9:28 PM
    Moderator
  • try this:

    add a reference to System.DirectoryServices. Then add this code:



    using System.DirectoryServices;
    ...
    ...
    private bool DoCreateUserAccount(string username, string password, string description)
    {
       try
       {
     
          DirectoryEntry theHost = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
          DirectoryEntry theUserAccount = theHost.Children.Add(username, "user");
     
          theUserAccount.Invoke("SetPassword", new object[] { password } );
          theUserAccount.Invoke("Put", new object[] { "Description", description } );
          theUserAccount.CommitChanges();
     
          return true;
     
       }
       catch (DirectoryServicesCOMException ex)
       {
          //handle the error
          return false;
       }
    }

     

     

    To add the user account into a specific group, add the following:

     



       DirectoryEntry theGroup;
       theGroup = theHost.Children.Find(GroupName, "group");
       if (theGroup != null)
       {
          theGroup.Invoke("Add", new object[] { theUserAccount.Path.ToString() } );
       }

     

    Tuesday, September 5, 2006 9:38 PM
    Moderator
  • ohhh no

    I want it to be by using WMI methods

    thanks

    Tuesday, September 5, 2006 10:04 PM
  • I think you may still get the same errors as you will when using the DirectoryServices approach, about the account not having permissions.

    the other, bad way, of doing it would be to execute a shell process of net use, to create the user account but thats all I can think of. As said previous, I can only find 1 method in WMI in the user_Accounts class, and thats to rename the account.

    Tuesday, September 5, 2006 10:19 PM
    Moderator