Skip to content

Commit

Permalink
Create ldapquery.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Un1k0d3r committed Jul 11, 2019
1 parent c260f78 commit e7f064b
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions ldapquery.cs
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,...");
}
}
}
}

0 comments on commit e7f064b

Please sign in to comment.