, , .
, , , adspath, Console.WriteLine .
# , IEnumerator DirectoryEntry, PrincipleSearcher , ,
, , . , .
. / powershell , Visual Studio.
$Source = @"
// " " <-- this just makes the code highlighter work
// Syntax: [soexample.search]::Get("LDAP Path", "property1", "property2", "etc...")
// Example: [soexample.search]::Get("LDAP:
namespace soexample
{
using System;
using System.DirectoryServices;
public static class search
{
public static string Get(string ldapPath, params string[] propertiesToLoad)
{
DirectoryEntry entry = new DirectoryEntry(ldapPath);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.SearchScope = SearchScope.OneLevel;
foreach (string p in propertiesToLoad) { searcher.PropertiesToLoad.Add(p); }
searcher.PageSize = 100;
searcher.SearchRoot = entry;
searcher.CacheResults = true;
searcher.Filter = "(sAMAccountType=805306368)";
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
foreach (string propertyName in propertiesToLoad)
{
foreach (object propertyValue in result.Properties[propertyName])
{
Console.WriteLine(string.Format("{0} : {1}", propertyName, propertyValue));
}
}
Console.WriteLine("");
}
return "";
}
}
}
"@
$Asem = ('System.DirectoryServices','System')
Add-Type -TypeDefinition $Source -Language CSharp -ReferencedAssemblies $Asem
, 160 , :
example :
PS > Measure-Command { [soexample.search]::Get(args as above..) }
:
givenname : John
sn : Surname
samaccountname : john.surname
distinguishedname : CN=John Surname,CN=Users,DC=mydomain,DC=com
etc ... 159 more ...
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 431
Ticks : 4317575
TotalDays : 4.99719328703704E-06
TotalHours : 0.000119932638888889
TotalMinutes : 0.00719595833333333
TotalSeconds : 0.4317575
TotalMilliseconds : 431.7575
, , 100 .
samaccountname 0,1 , 160 , .
Microsoft , , 3 , .
:
(sAMAccountType=805306368) , (&(objectClass=user)(objectCategory=person)) (. fooobar.com/questions/266182/...)
searcher.CacheResults = true;, , ( ), .
searcher.PageSize = 100; . , MaxPageSize DC 2012R2 1000 (https://technet.microsoft.com/en-us/library/cc770976(v=ws.11).aspx)
(.. , , result.Properties.PropertyNames, foreach propertiesToLoad)
foreach , , .
, , , , DirectorySearcher, , , System System.DirectoryServices .
, "//do stuff" , , , , - .