Created
January 14, 2018 19:39
-
-
Save leachdaniel/127c27f781c7451170d59266df56abb7 to your computer and use it in GitHub Desktop.
Query a Remote Domain Controller in C# with Powershell Script
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
$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