国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#獲取AD域中計算機和用戶的信息

2019-11-17 03:07:37
字體:
來源:轉載
供稿:網友

C#獲取AD域中計算機和用戶的信息

如果你的計算機加入了某個AD域,則可以獲取該域中所有的計算機和用戶的信息。

所用程序集,需要.Net Framework 4。

添加程序集引用System.DirectoryServices.AccountManagement

獲取AD域名稱,未加入AD域的,只能獲取計算機名稱。

如果未加入任何域,則后續的獲取域用戶、計算機等操作將無法進行,實例化域上下文對象時,會拋出異常。

1 ipGlobalPRoperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();2 string hostName = ipGlobalProperties.HostName;3 string domainName = ipGlobalProperties.DomainName;

獲取指定域中的用戶的查找對象。

1 PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainName);2 UserPrincipal userPrincipal = new UserPrincipal(principalContext);3 PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal);

查找域中用戶及其信息。

 1 StringBuilder sb = new StringBuilder();  2 foreach (UserPrincipal userPrincipalSearchResult in principalSearcher.FindAll()) 3 { 4     sb.AppendLine(string.Format("UPN:{0}", userPrincipalSearchResult.UserPrincipalName)); 5     sb.AppendLine(string.Format("姓氏Last Name:{0}", userPrincipalSearchResult.Surname)); 6     sb.AppendLine(string.Format("中間名:{0}", userPrincipalSearchResult.MiddleName)); 7     sb.AppendLine(string.Format("Given Name/First Name名:{0}", userPrincipalSearchResult.GivenName)); 8     sb.AppendLine(string.Format("名稱:{0}", userPrincipalSearchResult.Name)); 9     sb.AppendLine(string.Format("上次登錄時間:{0}", userPrincipalSearchResult.LastLogon));10 }11 userPrincipal.Dispose();12 Console.WriteLine(sb.ToString());

查找域中計算機及其信息,類似于查找用戶。首先使用域上下文對象實例化一個計算機對象,然后使用該對象實例化一個查找對象。

查找的結果,即為計算機對象,循環獲取信息即可。

 1 ComputerPrincipal computerPrincipal = new ComputerPrincipal(principalContext); 2 principalSearcher = new PrincipalSearcher(computerPrincipal); 3 //  4 foreach (ComputerPrincipal computerPrincipalSearchResult in principalSearcher.FindAll()) 5 { 6     sb.AppendLine(string.Format("UPN:{0}", computerPrincipalSearchResult.UserPrincipalName)); 7     sb.AppendLine(string.Format("描述:{0}", computerPrincipalSearchResult.Description)); 8     sb.AppendLine(string.Format("是否啟用:{0}", computerPrincipalSearchResult.Enabled)); 9     sb.AppendLine(string.Format("SAM賬號名:{0}", computerPrincipalSearchResult.SamAccountName));10     sb.AppendLine(string.Format("名稱:{0}", computerPrincipalSearchResult.Name));11     sb.AppendLine(string.Format("上次登錄時間:{0}", computerPrincipalSearchResult.LastLogon));12 }13 computerPrincipal.Dispose();14 Console.WriteLine(sb.ToString());

參考鏈接:http://www.codeproject.com/Articles/489348/Active-Directory-Users-and-Computers


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保康县| 平谷区| 辽阳县| 定陶县| 海盐县| 区。| 定远县| 射阳县| 高唐县| 白沙| 娄烦县| 嘉义县| 江城| 商南县| 长沙市| 淄博市| 新乡县| 临海市| 武定县| 新乡市| 新河县| 六枝特区| 辽阳县| 瑞昌市| 石门县| 磴口县| 田林县| 临西县| 六安市| 永顺县| 兴宁市| 独山县| 蓬莱市| 南陵县| 瓦房店市| 满城县| 右玉县| 巴东县| 南投县| 调兵山市| 文水县|