tags:

views:

48

answers:

0

The code below should rename a computer already connected to the domain. Basically when a user logs on a winform program is run and the user is prompted to enter a new computer name (this is made up from room number etc), the computer is already joined to the domain. The code below should check there is no computer already in the domain with the new name they want to use, if it does then the program should delete the computer from the domain (I know this seems heavy handed but it's fine) and then rename the computer. A typical name before it's renamed is ST0002ABC010 and the new name would be PC123-12. The user running the program is a domain admin at the moment whilst testing so it shouldn't be a straight forward permissions issue.

My problem is that all appears to work fine apart from the computer is not renamed, after a reboot the computer has the same name. Any help would be appreciated.

public partial class Form1 : Form { public static string currentPCname; public static string newPCname; public static string strdistinguishedName;

    public Form1()
    {
        InitializeComponent();    
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        currentPCname = System.Environment.MachineName;
        lblCurrentName.Text = currentPCname;
    }


    private void textBoxBnumber_TextChanged(object sender, EventArgs e)
    {
         newPCname = textBoxBnumber.Text + "-" + textBoxRoomNumber.Text;
         lblName.Text = newPCname;
    }

    private void textBoxRoomNumber_TextChanged(object sender, EventArgs e)
    {
        newPCname = textBoxBnumber.Text + "-" + textBoxRoomNumber.Text;
        lblName.Text = newPCname;
    }

    bool IsExistInAD(string newName)
    {
        DirectorySearcher search = new DirectorySearcher();
        search.Filter = String.Format("(SAMAccountName={0}$)", newPCname);
        search.PropertiesToLoad.Add("cn");
        search.PropertiesToLoad.Add("distinguishedName");
        SearchResult result = search.FindOne();

        if (result == null)
        {
            return false;
        }
        else
        {
            strdistinguishedName = result.Path.ToString();
            return true;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
            try
            {
                if (IsExistInAD(newPCname))
                {
                    try
                    {
                        string NewString = strdistinguishedName.Remove(7, 13);
                        DirectoryEntry AD = new DirectoryEntry(NewString);
                        DirectoryEntry NewComputer = AD.Children.Find("CN=" + newPCname, "computer");
                        AD.Children.Remove(NewComputer);
                        AD.CommitChanges();
                        AD.Close();
                        MessageBox.Show("Old computer deleted.");
                    }
                    catch
                    {
                        MessageBox.Show("A computer with the same name already exists and the program can't delete it");
                    }
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message.ToString());
            }
            try
            {
                String[] NewName = { newPCname };
                ManagementObject ComputerSystem = new ManagementObject(@"root\cimv2:Win32_ComputerSystem.Name='" + currentPCname + @"'");
                ComputerSystem.InvokeMethod("Rename", NewName);
                 System.Diagnostics.Process.Start("ShutDown", "/r");
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred, " + ex.Message.ToString());
            }
        }
    }