综合

img perky_zhou

使用C#批量修改域帐户信息全部代码

发表于2004/10/12 16:03:00  1270人阅读

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.DirectoryServices;

namespace change
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 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.Forms.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;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  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.Forms.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.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
   //
   // 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 = "选择部门(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 = "选择公司(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 = "新公司名称";
   //
   // 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 = "新部门名称";
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(376, 376);
   this.button1.Name = "button1";
   this.button1.TabIndex = 15;
   this.button1.Text = "开始处理";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.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.TabIndex = 20;
   this.checkBox1.Text = "修改公司名称";
   //
   // checkBox2
   //
   this.checkBox2.Location = new System.Drawing.Point(200, 192);
   this.checkBox2.Name = "checkBox2";
   this.checkBox2.TabIndex = 21;
   this.checkBox2.Text = "修改部门名称";
   //
   // checkBox3
   //
   this.checkBox3.Location = new System.Drawing.Point(336, 192);
   this.checkBox3.Name = "checkBox3";
   this.checkBox3.TabIndex = 22;
   this.checkBox3.Text = "修改部门领导";
   //
   // 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 = "新的部门领导";
   //
   // textBox4
   //
   this.textBox4.Location = 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 = "请输入新领导的姓名全拼(域帐号)";
   //
   // 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.Text = "选择二级子部门(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);
   this.label7.TabIndex = 26;
   this.label7.Text = "选择三级子部门(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 = "选择级五子部门(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_SelectedIndexChanged);
   //
   // 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 = "选择四级子部门(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(this.checkBox2);
   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 = "按OU成批修改信息";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [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+"/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+"--------------next------------------------/r/n";
    // MessageBox.Show("finish!!!");
   }
   catch(Exception ex)
   {
    
    MessageBox.Show(ex.Message);
   }
   finally
   {}
  
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   //初始化公司选择框
   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+"--------------next------------------------/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="请选择合适的OU";
    goto e1;
   }
   if(checkBox1.Checked)
   {
    
    if(textBox1.Text=="")
    {
     
     strErr="请输入新公司名称";
     
     goto e1;

    }
  
   }
   if(checkBox2.Checked)
   {
    
    if(textBox2.Text=="")
    {
     strErr="请输入部门名称!";
     goto e1;
    }
   }
   if(checkBox3.Checked)
   {
    if(textBox4.Text=="请输入新领导的姓名全拼(域帐号)")
    {
     strErr="请输入新领导的姓名全拼(域帐号)!";
     goto e1;

    }
    // string strNewManager="";
     //搜索
    //检查是否有重复的帐号
    DirectoryEntry su=new DirectoryEntry("LDAP://DC=test,DC=net");
    DirectorySearcher searcher = new DirectorySearcher();
    searcher.SearchRoot=su;
    searcher.PropertiesToLoad.Add("CN");
    searcher.Filter = "(&(objectClass=user)(sAMAccountName="+textBox4.Text.ToString()+"))";
    searcher.SearchScope = SearchScope.Subtree;
    searcher.Sort = new SortOption("sAMAccountName", SortDirection.Ascending);
    SearchResultCollection results = searcher.FindAll();
    DialogResult dlResult;
    string strMess="您确认要将该OU下的用户的领导更改为:"+results[0].Properties["CN"][0].ToString();
    dlResult=MessageBox.Show(this,strMess,"请确认",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="请重新输入新领导的姓名全拼(域帐号)!";
     strErr="请重新输入新领导的姓名全拼(域帐号)!";
     goto e1;
    }
   }
   if((!checkBox3.Checked)&&(!checkBox2.Checked)&&(!checkBox1.Checked))
   {
    strErr="请选择要修改的项目!";
    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(checkBox3.Checked)
      {
       if(chm.Properties.Contains("manager"))
       {
        chm.Properties["manager"][0]=strManager;
        chm.CommitChanges();
        textBox3.Text=textBox3.Text+chm.Name .ToString()+"的领导修改成功!/r/n";
        
       }
       else
       {
        chm.Properties["manager"].Add(strManager);
        chm.CommitChanges();
        textBox3.Text=textBox3.Text+chm.Name .ToString()+"的领导添加成功!/r/n";

       }
      }
     //修改公司
     if(checkBox1.Checked)
     {
      if(chm.Properties.Contains("company"))
      {
       chm.Properties["company"][0]=textBox1.Text.ToString();
       chm.CommitChanges();
       textBox3.Text=textBox3.Text+chm.Name .ToString()+"的公司名称修改成功!/r/n";
      }
      else
      {
       chm.Properties["company"].Add(textBox1.Text.ToString());
       chm.CommitChanges();
       textBox3.Text=textBox3.Text+chm.Name .ToString()+"的公司名称添加成功!/r/n";
       //textBox3.Text=textBox3.Text+ch1.Name .ToString()+"/r/n";
      }
     }
   
     //修改部门
     if(checkBox2.Checked)
     {
      if(chm.Properties.Contains("department"))
      {
       chm.Properties["department"][0]=textBox2.Text.ToString();
       chm.CommitChanges();
       textBox3.Text=textBox3.Text+chm.Name .ToString()+"的部门名称修改成功!/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()+"的部门名称添加成功!/r/n";
      }
     }
    
      
     
     }
     
    }
    MessageBox.Show("修改成功!");
   }
   catch(Exception ex)
   {
    strErr=ex.Message;
    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.Items.Add(ch1.Name.ToString());
     }
    }
      
    de1.Close();
   
    //textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";
    // MessageBox.Show("finish!!!");
   }
   catch(Exception ex)
   {
    
    MessageBox.Show(ex.Message);
   }
  }

  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.Items.Add(ch1.Name.ToString());
     }
    }
      
    de1.Close();
   
    //textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";
    // MessageBox.Show("finish!!!");
   }
   catch(Exception ex)
   {
    
    MessageBox.Show(ex.Message);
   }
  }

  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+"--------------next------------------------/r/n";
    // MessageBox.Show("finish!!!");
   }
   catch(Exception ex)
   {
    
    MessageBox.Show(ex.Message);
   }
  }

  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.Items.Clear();
    comboBox5.Text="";
    comboBox5.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")
     {
      comboBox5.Items.Add(ch1.Name.ToString());
     }
    }
      
    de1.Close();
   
    //textBox1.Text=textBox1.Text+"--------------next------------------------/r/n";
    // MessageBox.Show("finish!!!");
   }
   catch(Exception ex)
   {
    
    MessageBox.Show(ex.Message);
   }
  
  }

  private void comboBox5_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   str=comboBox5.SelectedItem.ToString()+","+comboBox6.SelectedItem.ToString()+","+comboBox4.SelectedItem.ToString()+","+
    comboBox3.SelectedItem.ToString()+","+comboBox2.SelectedItem.ToString()+","+
    comboBox1.SelectedItem.ToString();
  
  }

  
  
 }
 
}

阅读全文
0 0

相关文章推荐

img
取 消
img