public static void GetResponse(LdapConnection conn, string filter, SearchScope scope, string[] attrsToReturn, string dn, string printOption = null, string spnName = null) //Dictionary<string, string> myNames = null) { var request = new SearchRequest(dn, filter, scope, attrsToReturn); // the size of each page var pageReqControl = new PageResultRequestControl(500); // turn off referral chasing so that data // from other partitions is not returned //var searchControl = new SearchOptionsControl(SearchOption.DomainScope); //Unhandled Exception: System.ComponentModel.InvalidEnumArgumentException: //The value of argument 'value' (0) is invalid for Enum type 'SearchOption'. var searchControl = new SearchOptionsControl(); request.Controls.Add(pageReqControl); request.Controls.Add(searchControl); SearchResponse response; PageResultResponseControl pageResControl; // loop through each page while (true) { try { response = (SearchResponse)conn.SendRequest(request); if (response.Controls.Length != 1 || !(response.Controls[0] is PageResultResponseControl)) { Console.WriteLine("The server does not support this advanced search operation"); return; } pageResControl = (PageResultResponseControl)response.Controls[0]; //Console.WriteLine("\nThis page contains {0} response entries:\n", response.Entries.Count); switch (printOption) { //if there's only one attribute needs to be returned //and this attribute is a single-valued attribute case "single": Outputs.PrintSingle(response, attrsToReturn[0]); break; //if there's only one attribute needs to be returned //and this attribute is a multi-valued attribute case "multi": Outputs.PrintMulti(response, attrsToReturn[0]); break; ////Use specified name paris //case "mynames": //Outputs.PrintMyName(response, myNames); //break; case "gpo": Outputs.PrintGPO(response); break; case "spn": Outputs.PrintSPNs(response, spnName); break; case "domain": Outputs.PrintDomainAttrs(response); break; //case "attrname": //Outputs.PrintAttrName(response); //break; //default: print all attributesToReturned default: Outputs.PrintAll(response); break; } if (pageResControl.Cookie.Length == 0) { break; } pageReqControl.Cookie = pageResControl.Cookie; } catch (Exception e) { Console.WriteLine("Unexpected error: {0}", e.Message); break; } } }
public static void GetAppliedGPOs(LdapConnection connection, string rootDn, string name, bool isPC = false) { //if it is a computer account or a user account string nFilter = isPC ? @"(&(sAMAccountType=805306369)(name=" + name + "))" : @"(&(sAMAccountType=805306368)(name=" + name + "))"; string[] nAttrs = { "distingushiedName" }; //get the account distingushied name string Dn = GetSingleValue(connection, nFilter, SearchScope.Subtree, nAttrs, rootDn); Console.WriteLine(" * DN: {0}\n", Dn); //If Last OU/Domain blocks inheritance bool isBlocking = false; string dn = "CN=" + name + ","; string ou = Dn.Replace(dn, ""); //OU will not be affected by the block rule on itself int blockCounter = 0; try { while (ou.Contains(",")) { using (var entry = new DirectoryEntry("LDAP://" + ou)) { isBlocking = Outputs.PrintGplink(entry, ou, isBlocking, blockCounter); if (isBlocking) { blockCounter += 1; } } if (ou.Contains(",")) { ou = ou.Substring(ou.IndexOf(",") + 1); } else { break; } } }catch (Exception e) { Console.WriteLine(e.Message); } //get GPO applied on the site if (isPC) { try { string site = ActiveDirectorySite.GetComputerSite().Name; string siteDn = "CN=" + site + ",CN=Sites,CN=Configuration," + rootDn; using (var entry = new DirectoryEntry("LDAP://" + siteDn)) { Outputs.PrintGplink(entry, siteDn, isBlocking, blockCounter); } } catch { } } }