在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
应用技术:DirectoryServices、DataTable、Ext.Net(数据和相关控件、事件与后台C#交互)、数据库操作、LDAP 需求背景:公司应用了Windows2008的域(从2003升级),所有的电脑都加入了网域,但是维护人员常更换,有时也没有认真填写电脑在网域中的描述信息,有的人员离职了或更换工作地点、部门、电脑也可能更换了,但是都没有更新(好像是咱公司网管工作不到位)。现在由于特别的需要,要得到当前网域中在中国区的所有电脑名,包括描述、LDAP地址、所在的OU等信息。 需求内容:给出一个OU的LDAP地址,取出其所有的(包括子OU和组)电脑信息,在WEB中可以实时查询,以EXT做前台显示。可以通过前台实现更新CN下的所有电脑信息到数据库表中,以便信息应用。 实际图片: 前台实时查询
保存到数据库的信息 设计:用一个类实现取指定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="btnAddNe |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论