• 基于工作实际需求的Ext.Net和C#搭配应用之一 取出网域(AD)中所有计算机名及位置描述等信息


    应用技术:DirectoryServices、DataTable、Ext.Net(数据和相关控件、事件与后台C#交互)、数据库操作、LDAP

    需求背景:公司应用了Windows2008的域(从2003升级),所有的电脑都加入了网域,但是维护人员常更换,有时也没有认真填写电脑在网域中的描述信息,有的人员离职了或更换工作地点、部门、电脑也可能更换了,但是都没有更新(好像是咱公司网管工作不到位)。现在由于特别的需要,要得到当前网域中在中国区的所有电脑名,包括描述、LDAP地址、所在的OU等信息。

    需求内容:给出一个OU的LDAP地址,取出其所有的(包括子OU和组)电脑信息,在WEB中可以实时查询,以EXT做前台显示。可以通过前台实现更新CN下的所有电脑信息到数据库表中,以便信息应用。

    实际图片:

    前台实时查询

    clip_image001

    保存到数据库的信息

    clip_image001[4]

    设计:用一个类实现取指定LDAP地址下的所有电脑名及信息;用一个页面做实时查询,EXT做前台显示;用一个地址实现数据更新到数据库指定的表中。

    实现过程:

    建立一个类用于存储电脑信息(电脑名、描述、操作系统的名称和版本、登录的时间、加入网域的时间和更改的时间),PC.CS内容如下:

       1:  using System;
       2:  using System.Collections.Generic;
       3:  using System.Linq;
       4:  using System.Text;
       5:   
       6:  namespace Core.DarrenAdHelper
       7:  {
       8:      /// <summary>
       9:      /// 描述:
      10:      /// 程序員:谢堂文(Darren Xie)
      11:      /// 創建日期:
      12:      /// 版本:1.0
      13:      /// </summary>
      14:      public class PC
      15:      {
      16:          string cn;
      17:          /// <summary>
      18:          /// 电脑名
      19:          /// </summary>
      20:          public string Cn
      21:          {
      22:              get { return cn; }
      23:              set { cn = value; }
      24:          }
      25:          string description;
      26:          /// <summary>
      27:          /// 描述信息
      28:          /// </summary>
      29:          public string Description
      30:          {
      31:              get { return description; }
      32:              set { description = value; }
      33:          }
      34:          string whenCreated;
      35:          /// <summary>
      36:          /// 加入网域的时间
      37:          /// </summary>
      38:          public string WhenCreated
      39:          {
      40:              get { return whenCreated; }
      41:              set { whenCreated = value; }
      42:          }
      43:          string whenChanged;
      44:          /// <summary>
      45:          /// 最后更改时间
      46:          /// </summary>
      47:          public string WhenChanged
      48:          {
      49:              get { return whenChanged; }
      50:              set { whenChanged = value; }
      51:          }
      52:          string lastLogoff;
      53:          /// <summary>
      54:          /// 最后注消时间
      55:          /// </summary>
      56:          public string LastLogoff
      57:          {
      58:              get { return lastLogoff; }
      59:              set { lastLogoff = value; }
      60:          }
      61:          string lastLogon;
      62:          /// <summary>
      63:          /// 最后登录时间
      64:          /// </summary>
      65:          public string LastLogon
      66:          {
      67:              get { return lastLogon; }
      68:              set { lastLogon = value; }
      69:          }
      70:          string operatingSystem;
      71:          /// <summary>
      72:          /// 操作系统名称
      73:          /// </summary>
      74:          public string OperatingSystem
      75:          {
      76:              get { return operatingSystem; }
      77:              set { operatingSystem = value; }
      78:          }
      79:          string operatingSystemVersion;
      80:          /// <summary>
      81:          /// 操作系统版本
      82:          /// </summary>
      83:          public string OperatingSystemVersion
      84:          {
      85:              get { return operatingSystemVersion; }
      86:              set { operatingSystemVersion = value; }
      87:          }
      88:          string operatingSystemServicePack;
      89:          /// <summary>
      90:          /// 操作系统补丁
      91:          /// </summary>
      92:          public string OperatingSystemServicePack
      93:          {
      94:              get { return operatingSystemServicePack; }
      95:              set { operatingSystemServicePack = value; }
      96:          }
      97:          string distinguishedName;
      98:          /// <summary>
      99:          /// LDAP地址
     100:          /// </summary>
     101:          public string DistinguishedName
     102:          {
     103:              get { return distinguishedName; }
     104:              set { distinguishedName = value; }
     105:          }
     106:      }
     107:  }

    扫描AD信息取计算机名的类的关键代码如下:

       1:          #region 取出AD中的电脑名
       2:          public DataTable GetPCInfoTab()
       3:          {
       4:              return GetPCInfoTab(this.ADPath);
       5:          }
       6:          public DataTable GetPCInfoTab(string path)
       7:          {
       8:              return GetPCInfoTab(GetPC(path));
       9:          }
      10:          public DataTable GetPCInfoTab(List<Core.DarrenAdHelper.PC> l)
      11:          {
      12:              DataTable dt = new DataTable("pc");
      13:              dt.Columns.Add("cn");
      14:              dt.Columns.Add("Description");
      15:              dt.Columns.Add("OperatingSystem");
      16:              dt.Columns.Add("OperatingSystemVersion");
      17:              dt.Columns.Add("OperatingSystemServicePack");
      18:              dt.Columns.Add("WhenCreated");
      19:              dt.Columns.Add("WhenChanged");
      20:              dt.Columns.Add("LastLogon");
      21:              dt.Columns.Add("LastLogoff");
      22:              dt.Columns.Add("DistinguishedName");
      23:              foreach (Core.DarrenAdHelper.PC pc in l)
      24:              {
      25:                  DataRow dr = dt.NewRow();
      26:                  dr["cn"] = pc.Cn;
      27:                  dr["Description"] = pc.Description;
      28:                  dr["OperatingSystem"] = pc.OperatingSystem;
      29:                  dr["OperatingSystemVersion"] = pc.OperatingSystemVersion;
      30:                  dr["OperatingSystemServicePack"] = pc.OperatingSystemServicePack;
      31:                  dr["WhenCreated"] = pc.WhenCreated;
      32:                  dr["WhenChanged"] = pc.WhenChanged;
      33:                  dr["LastLogon"] = pc.LastLogon;
      34:                  dr["LastLogoff"] = pc.LastLogoff;
      35:                  dr["DistinguishedName"] = pc.DistinguishedName;
      36:                  dt.Rows.Add(dr);
      37:              }
      38:              return dt;
      39:          }
      40:          public List<Core.DarrenAdHelper.PC> GetPC()
      41:          {
      42:              return GetPC(this.ADPath);
      43:          }
      44:          public List<Core.DarrenAdHelper.PC> GetPC(string path)
      45:          {
      46:              using (DirectoryEntry de = new DirectoryEntry())
      47:              {
      48:   
      49:                  de.Path = path;
      50:                  List<Core.DarrenAdHelper.PC> l = new List<Core.DarrenAdHelper.PC>();
      51:   
      52:                  List<string> o = new List<string>();
      53:                  foreach (DirectoryEntry obj in de.Children)
      54:                  {
      55:                      if (obj.SchemaClassName == "computer" && obj.Name.Length >= 3)
      56:                      {
      57:                          Core.DarrenAdHelper.PC pc = new Core.DarrenAdHelper.PC();
      58:                          pc.Cn = obj.Properties.Contains("cn") == true ? obj.Properties["cn"].Value.ToString() : "-";
      59:                          pc.Description = obj.Properties.Contains("description") == true ? obj.Properties["description"].Value.ToString() : "-";
      60:                          pc.WhenCreated = obj.Properties.Contains("whenCreated") == true ? obj.Properties["whenCreated"].Value.ToString() : "-";
      61:                          pc.WhenChanged = obj.Properties.Contains("whenChanged") == true ? obj.Properties["whenChanged"].Value.ToString() : "-";
      62:                          pc.LastLogoff = obj.Properties.Contains("lastLogoff") == true ? obj.Properties["lastLogoff"].Value.ToString() : "-";
      63:                          pc.LastLogon = obj.Properties.Contains("lastLogon") == true ? obj.Properties["lastLogon"].Value.ToString() : "-";
      64:                          pc.OperatingSystem = obj.Properties.Contains("operatingSystem") == true ? obj.Properties["operatingSystem"].Value.ToString() : "-";
      65:                          pc.OperatingSystemVersion = obj.Properties.Contains("operatingSystemVersion") == true ? obj.Properties["operatingSystemVersion"].Value.ToString() : "-";
      66:                          pc.OperatingSystemServicePack = obj.Properties.Contains("operatingSystemServicePack") == true ? obj.Properties["operatingSystemServicePack"].Value.ToString() : "-";
      67:                          pc.DistinguishedName = obj.Properties.Contains("distinguishedName") == true ? obj.Properties["distinguishedName"].Value.ToString() : "-";
      68:                          l.Add(pc);
      69:                      }
      70:                      else if (obj.SchemaClassName == "organizationalUnit")
      71:                      {
      72:                          o.Add(obj.Path);
      73:   
      74:                      }
      75:   
      76:                  }
      77:                  foreach (string stroupath in o)
      78:                  {
      79:                      foreach (Core.DarrenAdHelper.PC pc1 in GetPC(stroupath))
      80:                      {
      81:                          l.Add(pc1);
      82:                      }
      83:                  }
      84:                  return l;
      85:              }
      86:          }
      87:   
      88:          #endregion

    页面前台代码:

    ADPCInfo.aspx

       1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ADPCInfo.aspx.cs" Inherits="ADPCInfo" %>
       2:   
       3:  <%@ Import Namespace="System.Data" %>
       4:  <%@ Import Namespace="System.Xml.Xsl" %>
       5:  <%@ Import Namespace="System.Xml" %>
       6:  <%@ Import Namespace="System.Linq" %>
       7:  <%@ Import Namespace="Ext" %>
       8:  <%@ Import Namespace="Core.DarrenCoreLib.DB" %>
       9:  <%@ Import Namespace="Core.DarrenEncodeOrDecode" %>
      10:  <%@ Import Namespace="Core.DarrenAdHelper" %>
      11:  <%@ Import Namespace="System.Collections.Generic" %>
      12:  <%@ Import Namespace="System.DirectoryServices" %>
      13:  <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
      14:   
      15:  <script runat="server">
      16:      private void GetInfo(object sender, DirectEventArgs e)
      17:      {
      18:          try
      19:          {
      20:              this.Store1.DataSource = this.GetDataTable();
      21:              this.Store1.DataBind();
      22:          }
      23:          catch (Exception ee)
      24:          {
      25:   
      26:              this.Store1.DataSource =  GetDataTableByErr(ee.Message);
      27:              this.Store1.DataBind();
      28:          }
      29:      }
      30:      public DataTable GetPCInfoTab(string path)
      31:      {
      32:          return GetPCInfoTab(GetPC(path));
      33:      }
      34:      public DataTable GetPCInfoTab(List<Core.DarrenAdHelper.PC> l)
      35:      {
      36:          DataTable dt = new DataTable("pc");        
      37:          dt.Columns.Add("cn");
      38:          dt.Columns.Add("Description");
      39:          dt.Columns.Add("OperatingSystem");
      40:          dt.Columns.Add("OperatingSystemVersion");
      41:          dt.Columns.Add("OperatingSystemServicePack");
      42:          dt.Columns.Add("WhenCreated");
      43:          dt.Columns.Add("WhenChanged");
      44:          dt.Columns.Add("LastLogon");
      45:          dt.Columns.Add("LastLogoff");
      46:          dt.Columns.Add("DistinguishedName");
      47:          dt.Columns.Add("spath");
      48:          foreach (Core.DarrenAdHelper.PC pc in l)
      49:          {
      50:              DataRow dr = dt.NewRow();
      51:              dr["cn"] = pc.Cn;
      52:              dr["Description"] = pc.Description;
      53:              dr["OperatingSystem"] = pc.OperatingSystem;
      54:              dr["OperatingSystemVersion"] = pc.OperatingSystemVersion;
      55:              dr["OperatingSystemServicePack"] = pc.OperatingSystemServicePack;
      56:              dr["WhenCreated"] = pc.WhenCreated;
      57:              dr["WhenChanged"] = pc.WhenChanged;
      58:              dr["LastLogon"] = pc.LastLogon;
      59:              dr["LastLogoff"] = pc.LastLogoff;
      60:              dr["DistinguishedName"] = pc.DistinguishedName;
      61:              string[] strpath = pc.DistinguishedName.Replace(",", "").Split(new string[] { "OU=", "DC=", "CN=" }, StringSplitOptions.RemoveEmptyEntries);
      62:              string spath = string.Empty;
      63:              for (int i = strpath.Length - 3; i >= 1; i--)
      64:              {
      65:                  spath += strpath[i] + @"\";
      66:              }
      67:              dr["spath"] = spath;
      68:              dt.Rows.Add(dr);
      69:          }
      70:          return dt;
      71:      }
      72:      public List<Core.DarrenAdHelper.PC> GetPC(string path)
      73:      {
      74:          using (DirectoryEntry de = new DirectoryEntry())
      75:          {
      76:   
      77:              de.Path = path;
      78:              System.Collections.Generic.List<Core.DarrenAdHelper.PC> l = new List<Core.DarrenAdHelper.PC>();
      79:   
      80:              List<string> o = new List<string>();
      81:              foreach (DirectoryEntry obj in de.Children)
      82:              {
      83:                  if (obj.SchemaClassName == "computer" && obj.Name.Length >= 3)
      84:                  {
      85:                      Core.DarrenAdHelper.PC pc = new Core.DarrenAdHelper.PC();
      86:                      pc.Cn = obj.Properties.Contains("cn") == true ? obj.Properties["cn"].Value.ToString() : "-";
      87:                      pc.Description = obj.Properties.Contains("description") == true ? obj.Properties["description"].Value.ToString() : "-";
      88:                      pc.WhenCreated = obj.Properties.Contains("whenCreated") == true ? obj.Properties["whenCreated"].Value.ToString() : "-";
      89:                      pc.WhenChanged = obj.Properties.Contains("whenChanged") == true ? obj.Properties["whenChanged"].Value.ToString() : "-";
      90:                      pc.LastLogoff = obj.Properties.Contains("lastLogoff") == true ? obj.Properties["lastLogoff"].Value.ToString() : "-";
      91:                      pc.LastLogon = obj.Properties.Contains("lastLogon") == true ? obj.Properties["lastLogon"].Value.ToString() : "-";
      92:                      pc.OperatingSystem = obj.Properties.Contains("operatingSystem") == true ? obj.Properties["operatingSystem"].Value.ToString() : "-";
      93:                      pc.OperatingSystemVersion = obj.Properties.Contains("operatingSystemVersion") == true ? obj.Properties["operatingSystemVersion"].Value.ToString() : "-";
      94:                      pc.OperatingSystemServicePack = obj.Properties.Contains("operatingSystemServicePack") == true ? obj.Properties["operatingSystemServicePack"].Value.ToString() : "-";
      95:                      pc.DistinguishedName = obj.Properties.Contains("distinguishedName") == true ? obj.Properties["distinguishedName"].Value.ToString() : "-";
      96:                      l.Add(pc);
      97:                  }
      98:                  else if (obj.SchemaClassName == "organizationalUnit")
      99:                  {
     100:                      o.Add(obj.Path);
     101:   
     102:                  }
     103:   
     104:              }
     105:              foreach (string stroupath in o)
     106:              {
     107:                  foreach (Core.DarrenAdHelper.PC pc1 in GetPC(stroupath))
     108:                  {
     109:                      l.Add(pc1);
     110:                  }
     111:              }
     112:              return l;
     113:          }
     114:      }
     115:      private System.Data.DataTable GetDataTable()
     116:      {
     117:          try
     118:          {
     119:              System.Collections.Generic.List<Core.DarrenAdHelper.PC> l = GetPC(path.Text + ",DC=cree1, DC=com");
     120:              
     121:              if(l.Count>0)
     122:              {
     123:                  return GetPCInfoTab(l);
     124:              }
     125:              else
     126:              {
     127:                  return GetDataTableByErr("沒有任何信息.");
     128:              }
     129:          }
     130:          catch (Exception ee)
     131:          {
     132:              try
     133:              {
     134:                  if (Session["sv"] == "")
     135:                  {
     136:                      return GetDataTableByErr("請從菜單中選取你要檢查的對象.");
     137:                  }
     138:                  else
     139:                  {
     140:                      return GetDataTableByErr(ee.Message);
     141:                  }
     142:              }
     143:              catch (Exception er)
     144:              {
     145:                  return GetDataTableByErr(er.Message);
     146:              }
     147:              
     148:          }
     149:      }
     150:      private System.Data.DataTable GetDataTableByErr(string errmsg)
     151:      {
     152:          try
     153:          {
     154:              DataTable dt = new DataTable("err");
     155:              
     156:             
     157:              DataColumn dc = new DataColumn("cn");
     158:   
     159:              dc.DataType = System.Type.GetType("System.String");
     160:              dc.DefaultValue = "ERR:"+errmsg;
     161:              dt.Columns.Add(dc);
     162:              DataRow dw = dt.NewRow();
     163:              dt.Rows.Add(dw);
     164:              return dt;
     165:          }
     166:          catch (Exception ee)
     167:          {
     168:              throw new Exception(ee.Message);
     169:          }
     170:      }
     171:      protected void Page_Load(object sender, EventArgs e)
     172:      {
     173:   
     174:          if (!X.IsAjaxRequest)
     175:          {
     176:              try
     177:              {
     178:                  this.Store1.DataSource = this.GetDataTable();
     179:                  this.Store1.DataBind();
     180:              }
     181:              catch (Exception ee)
     182:              {
     183:                  this.Store1.DataSource = this.GetDataTableByErr(ee.Message);
     184:                  this.Store1.DataBind();
     185:              }
     186:          }
     187:         
     188:          
     189:      }
     190:   
     191:      protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
     192:      {
     193:          this.Store1.DataSource = this.GetDataTable();
     194:          this.Store1.DataBind(); 
     195:      }
     196:   
     197:      protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
     198:      {
     199:          string format = this.FormatType.Value.ToString();
     200:   
     201:          XmlNode xml = e.Xml;
     202:   
     203:          this.Response.Clear();
     204:   
     205:          switch (format)
     206:          {
     207:              case "xml":
     208:                  string strXml = xml.OuterXml;
     209:                  this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xml");
     210:                  this.Response.AddHeader("Content-Length", strXml.Length.ToString());
     211:                  this.Response.ContentType = "application/xml";
     212:                  this.Response.Write(strXml);
     213:   
     214:                  break;
     215:              case "xls":
     216:                  this.Response.ContentType = "application/vnd.ms-excel";
     217:                  this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
     218:                  XslCompiledTransform xtExcel = new XslCompiledTransform();
     219:                  xtExcel.Load(Server.MapPath("Excel.xsl"));
     220:                  xtExcel.Transform(xml, null, Response.OutputStream);
     221:   
     222:                  break;
     223:              case "csv":
     224:                  this.Response.ContentType = "application/octet-stream";
     225:                  this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
     226:                  XslCompiledTransform xtCsv = new XslCompiledTransform();
     227:                  xtCsv.Load(Server.MapPath("Csv.xsl"));
     228:                  xtCsv.Transform(xml, null, Response.OutputStream);
     229:   
     230:                  break;
     231:          }
     232:   
     233:          this.Response.End();
     234:      }
     235:  </script>
     236:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     237:   
     238:  <html xmlns="http://www.w3.org/1999/xhtml">
     239:  <head runat="server">
     240:      <title>AD PC Info
     241:      </title>
     242:      <link href="../../../../resources/css/examples.css" rel="stylesheet" type="text/css" />
     243:   
     244:      <script type="text/javascript">
     245:          var template = '<span style="color:{0};">{1}</span>';
     246:   
     247:          var change = function (value) {
     248:              return String.format(template, (value > 0) ? "green" : "red", value);
     249:          };
     250:   
     251:          var pctChange = function (value) {
     252:              return String.format(template, (value > 0) ? "green" : "red", value + "%");
     253:          };
     254:   
     255:          var exportData = function (format) {
     256:              FormatType.setValue(format);
     257:              var store = GridPanel1.store;
     258:              store.directEventConfig.isUpload = true;
     259:   
     260:              var records = store.reader.readRecords(store.proxy.data).records,
     261:                  values = [];
     262:              
     263:              for (i = 0; i < records.length; i++) {
     264:                  var obj = {}, dataR;
     265:                  
     266:                  if (store.reader.meta.id) {
     267:                      obj[store.reader.meta.id] = records[i].id;
     268:                  }
     269:   
     270:                  dataR = Ext.apply(obj, records[i].data);                
     271:                  
     272:                  if (!Ext.isEmptyObj(dataR)) {
     273:                      values.push(dataR);
     274:                  }
     275:              }
     276:              
     277:              store.submitData(values);
     278:   
     279:              store.directEventConfig.isUpload = false;
     280:          };
     281:      </script>
     282:  </head>
     283:  <body>
     284:      <form id="Form1" runat="server">
     285:      <ext:ResourceManager ID="ResourceManager1" runat="server" />
     286:      <ext:Store ID="Store1" runat="server" AutoDataBind="true" remarks="" OnRefreshData="Store1_RefreshData"
     287:          OnSubmitData="Store1_Submit">
     288:          <Reader>
     289:              <ext:JsonReader>
     290:                  <Fields>
     291:                      <ext:RecordField Name="cn">
     292:                      </ext:RecordField>
     293:                      <ext:RecordField Name="Description">
     294:                      </ext:RecordField>
     295:                      <ext:RecordField Name="OperatingSystem">
     296:                      </ext:RecordField>
     297:                      <ext:RecordField Name="OperatingSystemVersion">
     298:                      </ext:RecordField>
     299:                      <ext:RecordField Name="OperatingSystemServicePack">
     300:                      </ext:RecordField>
     301:                      <ext:RecordField Name="WhenCreated">
     302:                      </ext:RecordField>
     303:                      <ext:RecordField Name="WhenChanged">
     304:                      </ext:RecordField>
     305:                      <ext:RecordField Name="LastLogon">
     306:                      </ext:RecordField>
     307:                      <ext:RecordField Name="LastLogoff">
     308:                      </ext:RecordField>
     309:                      <ext:RecordField Name="DistinguishedName">
     310:                      </ext:RecordField>
     311:                      <ext:RecordField Name="spath">
     312:                      </ext:RecordField>
     313:                  </Fields>
     314:              </ext:JsonReader>
     315:          </Reader>
     316:      </ext:Store>
     317:      <ext:Hidden ID="FormatType" runat="server" />
     318:      <ext:Panel ID="Panel1" runat="server" Height="470" Title="" Width="600">
     319:          <Items>
     320:              <ext:Toolbar ID="Toolbar2" runat="server" Width="600">
     321:                  <Items>
     322:                      <ext:TextField ID="path" runat="server" Text="LDAP://hzrdc01/OU=Huizhou2,OU=CN"  Width="500" MinWidth="200">
     323:                      </ext:TextField>
     324:                      <ext:Button ID="btnAddNewQaItem" runat="server" Text="更新信息">
     325:                          <DirectEvents>
     326:                              <Click OnEvent="GetInfo">
     327:                                  <EventMask MinDelay="10000" Msg="正在從後台查詢數據..." ShowMask="true" />
     328:                              </Click>
     329:                          </DirectEvents>
     330:                      </ext:Button>
     331:                  </Items>
     332:                 
     333:              </ext:Toolbar>
     334:              <ext:GridPanel ID="GridPanel1" runat="server" StoreID="Store1" Title="" Width="600"
     335:                  Height="430" AutoExpandColumn="cn" Icon="Mail">
     336:                  <ColumnModel ID="ColumnModel1" runat="server">
     337:                      <Columns>
     338:                          <ext:Column ColumnID="cn" Header="cn" Width="70" DataIndex="cn" Wrap="true">
     339:                              
     340:                              
     341:                          </ext:Column>
     342:                          <ext:Column ColumnID="Description" Header="Description" Width="150" Wrap="true"></ext:Column>
     343:                          <ext:Column ColumnID="OperatingSystem" Header="OperatingSystem" Width="100" Wrap="true">
     344:                          </ext:Column>
     345:                          <ext:Column ColumnID="OperatingSystemVersion" Header="OperatingSystemVersion" Width="50"
     346:                              Wrap="true">
     347:                          </ext:Column>
     348:                          <ext:Column ColumnID="OperatingSystemServicePack" Header="OperatingSystemServicePack"
     349:                              Width="150" Wrap="true">
     350:                          </ext:Column>
     351:                          <ext:Column ColumnID="WhenCreated" Header="WhenCreated" Width="70" Wrap="true">
     352:                          </ext:Column>
     353:                          <ext:Column ColumnID="WhenChanged" Header="WhenChanged" Width="70" Wrap="true">
     354:                          </ext:Column>
     355:                          <ext:Column ColumnID="LastLogon" Header="LastLogon" Width="70" Wrap="true" Hidden="true">
     356:                          </ext:Column>
     357:                          <ext:Column ColumnID="LastLogoff" Header="LastLogoff" Width="70" Wrap="true" Hidden="true">
     358:                          </ext:Column>
     359:                          <ext:Column ColumnID="DistinguishedName" Header="DistinguishedName" Width="150" Wrap="true">
     360:                          </ext:Column>                        
     361:                         <ext:Column ColumnID="spath" Header="spath" Width="150" Wrap="true">
     362:                          </ext:Column> 
     363:                      </Columns>
     364:                  </ColumnModel>
     365:                  <SelectionModel>
     366:                      <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" />
     367:                  </SelectionModel>
     368:                  <LoadMask ShowMask="true" />
     369:                  <TopBar>
     370:                      <ext:Toolbar ID="Toolbar1" runat="server">
     371:                          <Items>
     372:                              <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
     373:                              <ext:Button ID="Button1" runat="server" Text="To XML" Icon="PageCode">
     374:                                  <Listeners>
     375:                                      <Click Handler="exportData('xml');" />
     376:                                  </Listeners>
     377:                              </ext:Button>
     378:                              <ext:Button ID="Button2" runat="server" Text="To Excel" Icon="PageExcel">
     379:                                  <Listeners>
     380:                                      <Click Handler="exportData('xls');" />
     381:                                  </Listeners>
     382:                              </ext:Button>
     383:                              <ext:Button ID="Button3" runat="server" Text="To CSV" Icon="PageAttach">
     384:                                  <Listeners>
     385:                                      <Click Handler="exportData('csv');" />
     386:                                  </Listeners>
     387:                              </ext:Button>
     388:                          </Items>
     389:                      </ext:Toolbar>
     390:                  </TopBar>
     391:                  <BottomBar>
     392:                      <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="120" StoreID="Store1" />
     393:                  </BottomBar>
     394:              </ext:GridPanel>
     395:              <ext:TextArea ID="TextArea1" runat="server" Width="600" Height="380">
     396:              </ext:TextArea>
     397:          </Items>
     398:         
     399:      </ext:Panel>
     400:      </form>
     401:  </body>
     402:  </html>

    ADPCInfo2DB.aspx

       1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ADPCInfo2DB.aspx.cs" Inherits="ADPCInfo2DB" %>
       2:   
       3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       4:   
       5:  <html xmlns="http://www.w3.org/1999/xhtml">
       6:  <head runat="server">
       7:      <title></title>
       8:  </head>
       9:  <body>
      10:      <form id="form1" runat="server">
      11:      <div>
      12:      
      13:      </div>
      14:      </form>
      15:  </body>
      16:  </html>

    页面后台代码:

       1:  using System;
       2:  using System.Collections.Generic;
       3:  using System.Linq;
       4:  using System.Web;
       5:  using System.Web.UI;
       6:  using System.Web.UI.WebControls;
       7:  using System.DirectoryServices;
       8:  using System.Data;
       9:  using System.Data.SqlClient;
      10:   
      11:  public partial class ADPCInfo2DB : System.Web.UI.Page
      12:  {
      13:      protected void Page_Load(object sender, EventArgs e)
      14:      {
      15:          System.Collections.Generic.List<Core.DarrenAdHelper.PC> l = GetPC("LDAP://cnrdc01/OU=CN " + ",DC=cree1, DC=com");
      16:   
      17:          if (l.Count > 0)
      18:          {
      19:              SqlConnection conn = new SqlConnection(Core.DarrenEncodeOrDecode.EncodeOrDecode.Decode(System.Configuration.ConfigurationManager.ConnectionStrings["CotwapConnectionString"].ConnectionString));
      20:              string sql = sql = "delete PCInfo;";
      21:             
      22:                  if (conn.State != ConnectionState.Open)
      23:                      conn.Open();
      24:                  SqlTransaction st = conn.BeginTransaction();
      25:                  try
      26:                  {
      27:                      Core.DarrenCoreLib.DB.SqlHelper.ExecuteNonQuery(st, CommandType.Text, sql);
      28:   
      29:                      foreach (Core.DarrenAdHelper.PC pc1 in l)
      30:                      {
      31:                          sql = sql = "INSERT INTO [PCInfo]([Cn],[Description],[OperatingSystem],[OperatingSystemVersion],[OperatingSystemServicePack],[WhenCreated],[WhenChanged],[LastLogon],[LastLogoff],[DistinguishedName],[SPath]) ";
      32:                          string[] strpath = pc1.DistinguishedName.Replace(",", "").Split(new string[] { "OU=", "DC=", "CN=" }, StringSplitOptions.RemoveEmptyEntries);
      33:                          string spath = string.Empty;
      34:                          for (int i = strpath.Length - 3; i >= 1; i--)
      35:                          {
      36:                              spath += strpath[i] + @"\";
      37:                          }
      38:                          sql += " select '";
      39:                          sql += pc1.Cn + "','" + pc1.Description + "','" + pc1.OperatingSystem + "','" + pc1.OperatingSystemVersion + "','" + pc1.OperatingSystemServicePack + "','" + pc1.WhenCreated + "','" + pc1.WhenChanged + "','";
      40:                          sql += pc1.LastLogon + "','" + pc1.LastLogoff + "','" + pc1.DistinguishedName + "','" + spath + "' ";
      41:                          Core.DarrenCoreLib.DB.SqlHelper.ExecuteNonQuery(st, System.Data.CommandType.Text, sql);
      42:   
      43:                      }
      44:                      st.Commit();
      45:                  }
      46:                  catch
      47:                  {
      48:                      st.Rollback();
      49:                  }
      50:                  finally
      51:                  {
      52:                      if (conn.State != ConnectionState.Closed)
      53:                          conn.Close();
      54:                      Page.Response.Redirect("KillWindow.aspx");
      55:                  }
      56:          }
      57:      }
      58:      public DataTable GetPCInfoTab(string path)
      59:      {
      60:          return GetPCInfoTab(GetPC(path));
      61:      }
      62:      public DataTable GetPCInfoTab(List<Core.DarrenAdHelper.PC> l)
      63:      {
      64:          DataTable dt = new DataTable("pc");
      65:          dt.Columns.Add("cn");
      66:          dt.Columns.Add("Description");
      67:          dt.Columns.Add("OperatingSystem");
      68:          dt.Columns.Add("OperatingSystemVersion");
      69:          dt.Columns.Add("OperatingSystemServicePack");
      70:          dt.Columns.Add("WhenCreated");
      71:          dt.Columns.Add("WhenChanged");
      72:          dt.Columns.Add("LastLogon");
      73:          dt.Columns.Add("LastLogoff");
      74:          dt.Columns.Add("DistinguishedName");
      75:          dt.Columns.Add("spath");
      76:          foreach (Core.DarrenAdHelper.PC pc in l)
      77:          {
      78:              DataRow dr = dt.NewRow();
      79:              dr["cn"] = pc.Cn;
      80:              dr["Description"] = pc.Description;
      81:              dr["OperatingSystem"] = pc.OperatingSystem;
      82:              dr["OperatingSystemVersion"] = pc.OperatingSystemVersion;
      83:              dr["OperatingSystemServicePack"] = pc.OperatingSystemServicePack;
      84:              dr["WhenCreated"] = pc.WhenCreated;
      85:              dr["WhenChanged"] = pc.WhenChanged;
      86:              dr["LastLogon"] = pc.LastLogon;
      87:              dr["LastLogoff"] = pc.LastLogoff;
      88:              dr["DistinguishedName"] = pc.DistinguishedName;
      89:              string[] strpath = pc.DistinguishedName.Replace(",", "").Split(new string[] { "OU=", "DC=", "CN=" }, StringSplitOptions.RemoveEmptyEntries);
      90:              string spath = string.Empty;
      91:              for (int i = strpath.Length - 3; i >= 1; i--)
      92:              {
      93:                  spath += strpath[i] + @"\";
      94:              }
      95:              dr["spath"] = spath;
      96:              dt.Rows.Add(dr);
      97:          }
      98:          return dt;
      99:      }
     100:      public List<Core.DarrenAdHelper.PC> GetPC(string path)
     101:      {
     102:          using (DirectoryEntry de = new DirectoryEntry())
     103:          {
     104:   
     105:              de.Path = path;
     106:              System.Collections.Generic.List<Core.DarrenAdHelper.PC> l = new List<Core.DarrenAdHelper.PC>();
     107:   
     108:              List<string> o = new List<string>();
     109:              foreach (DirectoryEntry obj in de.Children)
     110:              {
     111:                  if (obj.SchemaClassName == "computer" && obj.Name.Length >= 3)
     112:                  {
     113:                      Core.DarrenAdHelper.PC pc = new Core.DarrenAdHelper.PC();
     114:                      pc.Cn = obj.Properties.Contains("cn") == true ? obj.Properties["cn"].Value.ToString() : "-";
     115:                      pc.Description = obj.Properties.Contains("description") == true ? obj.Properties["description"].Value.ToString() : "-";
     116:                      pc.WhenCreated = obj.Properties.Contains("whenCreated") == true ? obj.Properties["whenCreated"].Value.ToString() : "-";
     117:                      pc.WhenChanged = obj.Properties.Contains("whenChanged") == true ? obj.Properties["whenChanged"].Value.ToString() : "-";
     118:                      pc.LastLogoff = obj.Properties.Contains("lastLogoff") == true ? obj.Properties["lastLogoff"].Value.ToString() : "-";
     119:                      pc.LastLogon = obj.Properties.Contains("lastLogon") == true ? obj.Properties["lastLogon"].Value.ToString() : "-";
     120:                      pc.OperatingSystem = obj.Properties.Contains("operatingSystem") == true ? obj.Properties["operatingSystem"].Value.ToString() : "-";
     121:                      pc.OperatingSystemVersion = obj.Properties.Contains("operatingSystemVersion") == true ? obj.Properties["operatingSystemVersion"].Value.ToString() : "-";
     122:                      pc.OperatingSystemServicePack = obj.Properties.Contains("operatingSystemServicePack") == true ? obj.Properties["operatingSystemServicePack"].Value.ToString() : "-";
     123:                      pc.DistinguishedName = obj.Properties.Contains("distinguishedName") == true ? obj.Properties["distinguishedName"].Value.ToString() : "-";
     124:                      l.Add(pc);
     125:                  }
     126:                  else if (obj.SchemaClassName == "organizationalUnit")
     127:                  {
     128:                      o.Add(obj.Path);
     129:   
     130:                  }
     131:   
     132:              }
     133:              foreach (string stroupath in o)
     134:              {
     135:                  foreach (Core.DarrenAdHelper.PC pc1 in GetPC(stroupath))
     136:                  {
     137:                      l.Add(pc1);
     138:                  }
     139:              }
     140:              return l;
     141:          }
     142:      }
     143:      private System.Data.DataTable GetDataTable()
     144:      {
     145:          try
     146:          {
     147:              System.Collections.Generic.List<Core.DarrenAdHelper.PC> l = GetPC("LDAP://cnrdc01/OU=CN " + ",DC=cree, DC=com");
     148:   
     149:              if (l.Count > 0)
     150:              {
     151:                  return GetPCInfoTab(l);
     152:              }
     153:              throw new Exception("沒有信息.");
     154:          }
     155:          catch (Exception ee)
     156:          {
     157:              throw new Exception("沒有信息.");
     158:   
     159:          }
     160:      }
     161:  }

    其中还用到了自定义的加密解密类,对于配置文件,有一些企业的内容,不好放出来,只是给个方法。如果有兴趣可以一起讨论讨论。

  • 相关阅读:
    【大厂面试】字节跳动、京东等大厂面试题分享,已拿字节offer~
    听说这十道面试题,把百分之八十的程序员都淘汰了?不是吧,阿sir
    阿里大神的刷题笔记.pdf
    在家学了6个月,拿到阿里offer!
    【面试题】2020年底总结200道带答案的常见Java面试题!
    面试一家小公司,被问了 1 个小时并发编程。。全程已记录!
    分享一份技术书籍《Java 8 》PDF版,可自行下载
    Delphi的TDataSetProvider、TDataSet、TAdoQuery、TDataSource、TDataModule控件的组合使用
    初学Delphi与sql server的存储过程
    delphi简单的分页实现(高手可以不看)
  • 原文地址:https://www.cnblogs.com/yiyumeng/p/2358726.html
Copyright © 2020-2023  润新知