Use C # batch modification domain account information all code

xiaoxiao2021-03-06  80

Using system.drawing; using system.collections; using system.windows.form; using system.data; using system.directoryservices;

Summary description of Namespace Change {////

/// Form1. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.ComboBox comboBox2; private System.Windows.Forms.Label label3; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.Label label2; string str = ""; string strErr = ""; string strManager = ""; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label4; private System. Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.CheckBox checkBox1; private System. Windows.Forms.CheckBox checkBox2; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.ComboBox comboBox3; private System. Windows.Forms.Label Label6; Private System.Windows.Forms.comBobox ComboBox4; private system.windows.f orms.Label label7; private System.Windows.Forms.ComboBox comboBox5; private System.Windows.Forms.Label label8; private System.Windows.Forms.ComboBox comboBox6; private System.Windows.Forms.Label label9; /// // Required designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {Components.dispose ();}} Base.Dispose (DISPOSION);} #Region Windows Form The code generated by the designer /// /// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.comboBox2 = new System.Windows.Forms.ComboBox (); this.label3 = new System.Windows.Forms.Label (); this.comboBox1 = new System. Windows.Forms.comBobox (); this.label2 = new system.windows.forms.label (); this.label1 = new system.windows.forms.label (); this.label4 = new system.windows.forms.label (); This.Button1 = new system.windows.Forms.Button (); this.textbox1 = new system.windows.forms.textbox (); this.textbox2 = new system.windows.forms.textbox (); this. textBox3 = new System.Windows.Forms.TextBox (); this.checkBox1 = new System.Windows.Forms.CheckBox (); this.checkBox2 = new System.Windows.Forms.CheckBox (); this.checkBox3 = new System. Windows.Forms.CheckBox (); this.label5 = new System.Windows.Forms.Label (); this.textBox4 = new System.Windows.Forms.TextBox (); this.comboBox3 = new System.Windows.Forms.ComboBox (); This.label6 = new system.windows.forms.label (); this.comboBox4 = new system.windows.form S.combobox (); this.label7 = new system.windows.forms.label (); this.comboBox5 = new system.windows.forms.comBOBOX (); this.label8 = new system.windows.forms.label () This.comboBox6 = new system.windows.forms.combobox (); this.label9 = new system.windows.forms.label (); this.suspendlayout (); // // comboBoX2 // this.comboBox2.location = New system.drawing.point (416, 16); this.comboBox2.name = "combobox2"; this.comboBox2.size = new system.drawing.size (121, 20); this.comboBox2.tabindex = 10; this. ComboBoX2.Selected IndexChanged = new system.eventhandler (this.comboBox2_selected);

/// Label3 // this.label3.location = new system.drawing.point (320, 20); this.label3.name = "label3"; this.label3.size = new system.drawing.size (80, 16); this.Label3.Tabindex = 9; this.label3.text = "Select department (OU)"; // // ComboBox1 // this.comboBox1.location = new system.drawing.point (144, 16); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size (121, 20); this.comboBox1.TabIndex = 8; this.comboBox1.SelectedIndexChanged = new System.EventHandler (this. ComboBox1_selectedIndexchanged; // // label2 // this.label2.autosize = true; this.label2.location = new system.drawing.point (40, 19); this.label2.name = "label2"; this.label2. Size = new system.drawing.size (79, 17); this.label2.tabindex = 7; this.Label2.text = "Select Company (OU)"; // // Label1 // this.label1.Location = New System.drawing.Point (48, 128); this.label1.name = "label1"; this.label1.size = new system.drawing.size (72, 23); this.label1.tabindex = 11; this.label1.text = "New Company Name"; // // Label4 // this.label4.location = new system.drawing.point (328, 128); this.label4.name = "label4"; this .label4.size = new system.drawing.size (72, 23); this.label4.tabindex = 12; this.label4.text = "new department name"; // // button1 // this.button1.location = New system.drawing.point (376, 376); this.button1.name = "button1"; this.button1.tabindex = 15; this.button1.text = "Start processing"; this.button1.click = new system . EventHandler (this.button1_click);

//////////////////////////////Wwndrawing.point (144, 128); this.TextBox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (120, 21); this.TextBox1.TabINDEX = 16; this.textBox1.text = ""; /// textbox2 // this.textBox2.Location = new system.drawing.point (416, 128); this.textbox2.name = "textbox2"; this.textbox2.size = new system.drawing.size (120, 21); this.TextBox2.tabindex = 17; this.textBox2.text = ""; /// textbox3 // this.TextBox3 .Location = new System.Drawing.Point (32, 232); this.textBox3.Multiline = true; this.textBox3.Name = "textBox3"; this.textBox3.ReadOnly = true; this.textBox3.ScrollBars = System.Windows .Forms.scrollbars.both; this.textbox3.size = new system.drawing.size (504, 120); this.textbox3.tabindex = 18; this.textBox3.text = ""; /// checkbox1 // this .checkbox1.location = new system.drawing.point (64, 192); this.checkbox1.name = "checkbox1"; this.checkbox1.tabinde x = 20; this.checkbox1.text = "Modify the company name"; //// Checkbox2 // this.checkbox2.location = new system.drawing.point (200, 192); this.checkbox2.name = "checkbox2" This.checkbox2.tabindex = 21; this.checkbox2.text = "Modified Department Name"; /// CheckBox3 // this.checkbox3.location = new system.drawing.point (336, 192); this.checkbox3. Name = "checkbox3"; this.checkbox3.tabindex = 22; this.checkbox3.text = "Modify department leadership"; // // label5 // this.label5.autosize = true; this.label5.Location =

New system.drawing.point (40, 160); this.Label5.name = "label5"; this.label5.size = new system.drawing.size (79, 17); this.label5.tabindex = 19; this. Label5.text = "New Department Leaders"; ////////////////////////////////////////////////////////////////////////bitBox4.point = New System.drawing.Point (144, 160); this.TextBox4.name = "textbox4"; this.TextBox4. Size = new system.drawing.size (392, 21); this.textbox4.tabindex = 23; this.textbox4.text = "Please enter the new leader's name (domain account)"; // // ComboBoX3 // This.comboBox3.Location = new system.drawing.point (144, 48); this.comboBox3.name = "combobox3"; this.comboBox3.size = new system.drawing.size (121, 20); this.comboBOX3. TabIndex = 25; this.comboBox3.SelectedIndexChanged = new System.EventHandler (this.comboBox3_SelectedIndexChanged); // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point (4 51); this.Label6.Name = "label6"; this.label6.size = new system.drawing.size (116, 17); this.label6.tabindex = 24; this.label6.tex T = "Select the secondary sub-department (OU)"; /// ComboBoX4 // this.comboBox4.Location = new system.drawing.point (416, 48); this.comboBox4.name = "ComboBoX4"; this. comboBox4.Size = new System.Drawing.Size (121, 20); this.comboBox4.TabIndex = 27; this.comboBox4.SelectedIndexChanged = new System.EventHandler (this.comboBox4_SelectedIndexChanged); // // label7 // this. Label7.autosize = true; this.label7.location = new system.drawing.point (284, 51); this.label7.name = "label7"; this.label7.size = new system.drawing.size (116, 17 );

THISLABEL7.TABINDEX = 26; this.label7.text = "Select the three sub-department (OU)"; // // ComboBox5 // this.comboBOX5.Location = New System.drawing.Point (416, 80); this.comboBox5.Name = "comboBox5"; this.comboBox5.Size = new System.Drawing.Size (121, 20); this.comboBox5.TabIndex = 31; this.comboBox5.SelectedIndexChanged = new System.EventHandler (this. ComboBOX5_SELECTEDINDEXCHANGED); // // label8 // this.label8.Location = new system.drawing.point (280, 84); this.label8.name = "label8"; this.label8.size = new system.drawing.size (120, 16); this.Label8.Tabindex = 30; this.label8.text = "Select stage five sub-sector (ou)"; // // ComboBox6 // this.comboBox6.Location = new system.drawing.point ( 144, 80); this.comboBox6.Name = "comboBox6"; this.comboBox6.Size = new System.Drawing.Size (121, 20); this.comboBox6.TabIndex = 29; this.comboBox6.SelectedIndexChanged = new System . EventHandler (this.combobox6_selected); // // label9 // this.label9.autosize = true; this. Label9.location = new system.drawing.point (4, 83); this.label9.name = "label9"; this.label9.size = new system.drawing.size (116, 17); this.label9.tabindex = 28; this.label9.text = "Select the four sub-department (OU)"; // // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing .Size (584, 429); this.Controls.add (this.comboBox5); this.controls.add (this.label8); this.controls.add (this.comboBox6); this.controls.add (this.label9 ); This.Controls.add (this.comboBox4); this.Controls.add (this.label7);

This.Controls.add (this.comboBox3); this.controls.add (this.label6); this.Controls.add (this.TextBox4); this.Controls.add (this.checkbox3); this.Controls.add ( THISCHECKBOX2; this.Controls.add (this.checkbox1); this.controls.add (this.label5); this.controls.add (this.TextBox3); this.controls.add (this.TextBox2); this. .Controls.add (this.TextBox1); this.controls.add (this.button1); this.controls.add (this.label4); this.controls.add (this.label1); this.controls.add (this .comboBox2); this.controls.add (this.label3); this.Controls.add (this.comboBox1); this.controls.add (this.Label2); this.name = "form1"; this.text = " Press OU into batch modification information "; this.Load = new system.eventhandler (this.form1_load); this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());}

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 "/ roperties [" adpath "] [0] .tostring (); string str1 ="; str1 = ch1.schemaclassname.tostring () ; If (str1 == "Organization)) {ComboBox2.Items.add (ch1.name.tostring ());}} de1.close (); //textbox1.text=textBox1.text "-------- -------- Next ---------------------- / r / n "; // messagebox.show (" Finish !!! ");} Catch (exception ex) {messagebox.show (ex.Message);} finally {}}

Private void form1_load (object sender, system.eventargs e) {// Initializes company selection box DirectoryEntry de1 = new DirectoryEntry (); de1.path = "ldap: // DC = TEST, DC = Net"; try {foreach (DirectoryEntry CH1 in de1.children) {// textBox1.text = textBox1.text ch1.Name "/ r / n"; // ch.properties ["adpath"] [0] .tostring (); 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 ()); 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 {}}

Private void button1_click (object sender, system.eventargs e) {string stradroot = ""; string strname = ""; if (comboBoX1.text == "|| comboBox2.text ==") {strerr = "Please select Suitable OU "; goto E1;} f (checkbox1.checked) {if (TextBox1.text ==") {strerr = "Please enter the new company name"; goto E1;

}}}} {If (TextBox2.text == ") {strerr =" Please enter the department name! "; Goto E1;}} {if (TextBox4.Text ==" Please enter the new leader's name (domain account) ") {strerr =" Please enter the new leader's name full (domain account)! "; Goto E1;

} // string strnewmanager = ""; // Search // Check if there is a duplicate account DirectoryEntry Su = New DirectoryEntry ("LDAP: // DC = Test, DC = Net"); DirectorySearcher Searcher = new directorySearcher (); SEARCHER .SearchRoot = SU; Searcher.propertiestold.add ("cn"); Searcher.filter = "(SamaccountName =" TextBox4.text.toString () ")" ")"; Searcher.SearchScope = SearchScope.Subtree; searcher.Sort = new SortOption ( "sAMAccountName", SortDirection.Ascending); SearchResultCollection results = searcher.FindAll (); DialogResult dlResult; string strMess = "Do you want to change the user's OU under the leadership: " results [0] .Properties [" CN "] [0] .ToString (); dlResult = MessageBox.Show (this, strMess," Please confirm ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions. RightAlign); if (DLRESULT == DialogResult.yes) {strmanager = results [0] .properties ["adspath"] [0] .tostring (); strmanager = strmanager.remove (0, 7);} else {textbox4. TEXT = "Please re-enter the new leader's name full rules (domain account)! "; STRERR =" Please re-enter the new leader's name full (domain account)! "; Goto E1;}}} (! Checkbox3.checked) && (! Checkbox2.checked) && (! Checkbox1.checked)) {strerr =" Please select the item you want to modify! "Goto E1;

} Stradroot = "ldap: //" Str ", DC = TEST, DC = Net"; DIRECTORYENTRY DE = New DirectoryEntry (); de.path = stradroot; textbox3.text = ""; try {foreach (DirectoryEntry Chm in De.children) {string strtype = ""; strtype = chm.schemaclassname.tostring (); if (strtype.toupper () == "user") {strname = chm.name.tostring (); // If selected Modify the leadership if (checkbox3.checked) {if (chm.properties.contains) {chm.properties ["manager"] [0] = STRMANAGER; chm.commitchanges (); textbox3.text = TextBox3. Text chm.name.tostring () "leadership is successful! / R / n";} else {chm.properties ["manager"]. Add (strmanager); chm.commitchanges (); TextBox3.text = TextBox3 .Text chm.name.tostring () "leadership added success! / R / n";

}} // Modify the company if (checkbox1.checked) {if (chm.properties.contains ("company")) {chm.properties ["company"] [0] = textbox1.text.toString (); chm.commitchanges (); TextBox3.text = TextBox3.text chm.name .tostring () "company name modification success! / R / n";} else {chm.properties ["Company"]. Add (TextBox1.text. Tostring ()); chm.commitchanges (); TextBox3.text = TextBox3.text chm.name .tostring () "company name Add success! / R / n"; //textbox3.text=textBox3.Text CH1.NAME.TOSTRING () "/ r / n";}} // Modify department if (checkbox2.checked) {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 1.name .tostring () "/ r / n" TextBox3.Text = Textb Ox3.text chm.name.tostring () "department name is successful! / r / n ";}}}} messagebox.show (" Modified success! ");} catch (exception ex) {strerr = ex. amount; goto E1;} E1: if (STRERR! =") {MessageBox .Show (strerr);}}

private void comboBox2_SelectedIndexChanged (object sender, System.EventArgs e) {//str=listBox1.SelectedItem.ToString (); str = comboBox2.SelectedItem.ToString () "," comboBox1.SelectedItem.ToString (); DirectoryEntry de1 = New DirectoryEntry (); de1.path = "LDAP: //" Str ", DC = TEST, DC = Net"; try {comboBOX3.Items.clear (); comboBox3.text = "; comboBOX3.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") {comboBOX3.TSTRING ());}} de1.close (); // TextBox1. TEXT = TEXTBOX1.TEXT "------------------------------------ / r / n" ///////// 's "finish !!!");} catch (exception ex) {messagebox.show (ex. measureage);}}

private void comboBox3_SelectedIndexChanged (object sender, System.EventArgs e) {str = comboBox3.SelectedItem.ToString () "," comboBox2.SelectedItem.ToString () "," comboBox1.SelectedItem.ToString (); DirectoryEntry de1 = New DirectoryEntry (); de1.path = "LDAP: //" Str ", DC = TEST, DC = Net"; try {comboBox4.items.clear (); comboBox4.text = ""; comboBox4.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") {comboBox4.d ();}} de1.close ();}} de1.close (); // TextBox1. TEXT = TEXTBOX1.TEXT "------------------------------------ / r / n" ///////// 's "finish !!!");} catch (exception ex) {messagebox.show (ex. measureage);}}

private void comboBox4_SelectedIndexChanged (object sender, System.EventArgs e) {str = comboBox4.SelectedItem.ToString () "," comboBox3.SelectedItem.ToString () "," comboBox2.SelectedItem.ToString () "," ComboBox1.SelectedItem.toString (); DirectoryEntry de1 = new DirectoryEntry (); de1.path = "ldap: //" Str ", DC = TEST, DC = Net"; try {comboBOX6.Items.clear (); ComboBOX6.Text = ""; ComboBox6.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") {ComboBox6.Items.add (ch1.name.tostring ()); }} De1.close (); //textBox1.text=textBox1.text "----------------------- ------ / r / n "; // MessageBox.show (" Finish !!! ");} catch (exception ex) {messagebox.show (ex. measureage);}}

private void comboBox6_SelectedIndexChanged (object sender, System.EventArgs e) {str = comboBox6.SelectedItem.ToString () "," comboBox4.SelectedItem.ToString () "," comboBox3.SelectedItem.ToString () "," ComboBox2.SelectedItem.toString () "," ComboBox1.SelectedItem.toString (); DirectoryEntry de1 = new directoryEntry (); de1.path = "ldap: //" str ", DC = test, DC = Net "; Try {comboBox5.tems.clear (); combobox5.text =" "; combobox5.refresh (); foreach (directRyEntry Ch1 in de1.children) {// textbox1.text = textbox1.text str " / r / n "; // ch.properties [" adpath "] [0] .tostring (); string str1 =" "; str1 = cha1.schemaclassname.tostring (); if (str1 ==" OrganizationalUnit ") {ComboBox5.Items .Add (ch1.name.tostring ());}} de1.close (); //textbox1.text=textbox1.text "------------Next----- ------------------- / r / n "; // MessageBox.show (" Finish !!! ");} catch (exception ex) {messagebox.show EXMESSAGE);}} private void comboBOX5_SELE ctedIndexChanged (object sender, System.EventArgs e) {str = comboBox5.SelectedItem.ToString () "," comboBox6.SelectedItem.ToString () "," comboBox4.SelectedItem.ToString () "," comboBox3 .SelectedItem.toString () " ComboBox2.SelectedItem.toString () ", ComboBoX1.SelectedItem.toString ();

}

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

New Post(0)