-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c260f78
commit e7f064b
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.DirectoryServices; | ||
|
||
namespace LdapQuery | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
if (args.Length >= 3) | ||
{ | ||
string domain = args[0]; | ||
string filter = args[1]; | ||
string properties = args[2]; | ||
|
||
Console.WriteLine("Querying LDAP://{0}", domain); | ||
Console.WriteLine("Querying: {0}", filter); | ||
Console.WriteLine("Extracting: {0}", properties); | ||
DirectoryEntry de = new DirectoryEntry("LDAP://" + domain); | ||
DirectorySearcher ds = new DirectorySearcher(de); | ||
|
||
ds.Filter = filter; | ||
|
||
foreach (SearchResult r in ds.FindAll()) | ||
{ | ||
try | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
foreach (string prop in properties.Split(',')) | ||
{ | ||
sb.Append(r.Properties[prop].Count > 0 ? r.Properties[prop][0] : String.Empty); | ||
sb.Append(","); | ||
} | ||
Console.WriteLine(sb.ToString()); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine("ERROR: {0}", e.Message); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Missing arguments\nUsage: domain query properties,..."); | ||
} | ||
} | ||
} | ||
} |