Use C # batch modification domain account information

xiaoxiao2021-03-06  76

The company's organizational structure often changes, and our domain account information (

AD

) It corresponds to the real organizational organization. Organizational institutions change, we naturally change the corresponding domain account information. This is a very painful thing, what is the reason, everyone understands. So can you solve this problem with a program? (

Windows2003

The management tool seems to have supported bulk modification domain account information).

I created a Windows application to solve:

Six ComboBox is placed on the form to select the company's OU (you can choose 5 OU if you like TreeView more).

Initialize these ComboBox when loading FORM:

Private Void Form1_Load (Object Sender, System.EventArgs E)

{

// Initialize the company selection box

DirectoryEntry de1 = new directoryentry ();

DE1.PATH = "LDAP: // DC = TEST, DC = NET";

Try

{

// All OUs (first level)

Foreach (DirectoryEntry Ch1 in de1.children)

{

Str = ch1.Name.toString ();

String str1 = "";

//str1=str.substring(0 ,str.indexof("== "));

Str1 = ch1.schemaclassname.tostring ();

IF (str1 == "OrganizationalUnit")

{

//listbox1.items.add (ch1.name.tostring ());

// Join the first ComboBox

ComboBox1.Items.Add (ch1.name.tostring ());

//

ComboBox3.Items.Add (ch1.name.tostring ());

}

}

de1.close ();

//textbox1.text=textBox1.text "------------------------------ R / N ";

//

Messagebox.show ("finish !!!");

}

Catch (Exception EX)

{

STRERR = ex.Message;

}

Finally

{}

}

All of the first layer OU is displayed in the first ComboBox when the Form is initialized. Then, when this comboBox is selected, the next level is displayed in the second ComboBox:

Private void ComboBox1_selectedIndexchanged (Object Sender, System.Eventargs E)

{

//str=listbox1.selectedItem.toString ();

Str = comboBOX1.SelectedItem.toString ();

DirectoryEntry de1 = new directoryentry ();

DE1.PATH = "LDAP: //" Str ", DC = TEST, DC = Net";

Try

{

ComboBox2.Items.clear ();

ComboBox2.text = "";

ComboBOX2.Refresh ();

Foreach (DirectoryEntry Ch1 in de1.children)

{

//

TextBox1.text = textbox1.text str "/ r / n"; // ch.properties ["adpath"] [0] .tostring ();

String str1 = "";

Str1 = ch1.schemaclassname.tostring ();

IF (str1 == "OrganizationalUnit")

{

ComboBox2.Items.add (ch1.name.tostring ());

}

}

de1.close ();

//textbox1.text=textBox1.text "------------------------------ R / N ";

//

Messagebox.show ("finish !!!");

}

Catch (Exception EX)

{

Messagebox.show (ex.Message);

}

Finally

{}

}

The next level OU is displayed in the next ComboBox. After selecting the OU to modify, you can modify all the information under this OU. Here is the name of the modification department as an example.

Use a TextBox input department name, put a button to trigger the modification:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

String stradroot = "";

String strname = "";

// STR Save is part of the ADPATH of the OU, that is, by selecting the string generated by ComboBox, similar to OU = IMD, OU = Company

STROOT = "LDAP: //" Str ", DC = TEST, DC = Net"

DirectoryEntry de = new DirectoryEntry ();

de.path = stradroot;

/ / Modify all User objects

Foreach (DirectoryEntry Chm in de.children)

{

String string start = "";

Strtype = chm.schemaclassname.tostring ();

IF (strtype.toupper () == "user")

{

Strname = chm.name.tostring ();

/ / Judgment if the property value is empty

IF (chm.properties.contains ("department"))

{

Chm.properties ["department"] [0] = textbox2.text.toString ();

CHM.commitchanges ();

TextBox3.text = textbox3.text chm.name .tostring () "department name modification success! / r / n";

}

Else

{

CHM.Properties ["Department"]. Add (TextBox2.Text.toString ());

CHM.commitchanges ();

//TextBox3.text=textBox3.text ch1.name .tostring () "/ r / n";

TextBox3.text = textbox3.text chm.name .tostring () "department name is successful! / r / n";}

}

}

转载请注明原文地址:https://www.9cbs.com/read-107401.html

New Post(0)