Skip to content

Instantly share code, notes, and snippets.

@leachdaniel
Created January 14, 2018 19:39
Show Gist options
  • Save leachdaniel/127c27f781c7451170d59266df56abb7 to your computer and use it in GitHub Desktop.
Save leachdaniel/127c27f781c7451170d59266df56abb7 to your computer and use it in GitHub Desktop.
Query a Remote Domain Controller in C# with Powershell Script
$Assem = (
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.DirectoryServices.dll"
)
$Source = @”
using System.DirectoryServices;
using System;
namespace RemoteDomainAuthenticationTest
{
public static class TestIt
{
public static bool Test()
{
bool userOk = false;
string realName = string.Empty;
string name = "{username}";
using (DirectoryEntry directoryEntry =
new DirectoryEntry("LDAP://{domainControllerIP}/DC={subdomain},DC={domain},DC={domainsuffix}", name, "{password}"))
{
using (DirectorySearcher searcher = new DirectorySearcher(directoryEntry))
{
searcher.Filter = "(samaccountname=" + name + ")";
searcher.PropertiesToLoad.Add("displayname");
SearchResult adsSearchResult = searcher.FindOne();
if (adsSearchResult != null)
{
if (adsSearchResult.Properties["displayname"].Count == 1)
{
realName = (string)adsSearchResult.Properties["displayname"][0];
}
userOk = true;
}
}
}
return userOk;
}
}
}
“@
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
[RemoteDomainAuthenticationTest.TestIt]::Test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment