Last active
February 13, 2024 04:51
-
-
Save nullbind/9054b0324e2bd34010fdd646f6db5d6c to your computer and use it in GitHub Desktop.
Get-DomainObject.ps1
This file contains hidden or 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
function Get-LdapQuery | |
{ | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory = $false, | |
HelpMessage = 'Domain user to authenticate with domain\user.')] | |
[string]$Username, | |
[Parameter(Mandatory = $false, | |
HelpMessage = 'Domain password to authenticate with domain\user.')] | |
[string]$Password, | |
[Parameter(Mandatory = $false, | |
HelpMessage = 'Credentials to use when connecting to a Domain Controller.')] | |
[System.Management.Automation.PSCredential] | |
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty, | |
[Parameter(Mandatory = $false, | |
HelpMessage = 'Domain controller for Domain and Site that you want to query against.')] | |
[string]$DomainController, | |
[Parameter(Mandatory = $false, | |
HelpMessage = 'LDAP Filter.')] | |
[string]$LdapFilter = '', | |
[Parameter(Mandatory = $false, | |
HelpMessage = 'LDAP path.')] | |
[string]$LdapPath, | |
[Parameter(Mandatory = $false, | |
HelpMessage = 'Maximum number of Objects to pull from AD, limit is 1,000 .')] | |
[int]$Limit = 1000, | |
[Parameter(Mandatory = $false, | |
HelpMessage = 'scope of a search as either a base, one-level, or subtree search, default is subtree.')] | |
[ValidateSet('Subtree','OneLevel','Base')] | |
[string]$SearchScope = 'Subtree' | |
) | |
Begin | |
{ | |
# Create PS Credential object | |
if($Username -and $Password) | |
{ | |
$secpass = ConvertTo-SecureString $Password -AsPlainText -Force | |
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($Username, $secpass) | |
} | |
# Create Create the connection to LDAP | |
if ($DomainController) | |
{ | |
# Verify credentials were provided | |
if(-not $Username){ | |
Write-Output "A username and password must be provided when setting a specific domain controller." | |
Break | |
} | |
# Test credentials and grab domain | |
try { | |
$objDomain = (New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList "LDAP://$DomainController", $Credential.UserName, $Credential.GetNetworkCredential().Password).distinguishedname | |
}catch{ | |
Write-Output "Authentication failed." | |
} | |
# add ldap path | |
if($LdapPath) | |
{ | |
$LdapPath = '/'+$LdapPath+','+$objDomain | |
$objDomainPath = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList "LDAP://$DomainController$LdapPath", $Credential.UserName, $Credential.GetNetworkCredential().Password | |
} | |
else | |
{ | |
$objDomainPath = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList "LDAP://$DomainController", $Credential.UserName, $Credential.GetNetworkCredential().Password | |
} | |
$objSearcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ArgumentList $objDomainPath | |
} | |
else | |
{ | |
$objDomain = ([ADSI]'').distinguishedName | |
if($LdapPath) | |
{ | |
$LdapPath = $LdapPath+','+$objDomain | |
$objDomainPath = [ADSI]"LDAP://$LdapPath" | |
} | |
else | |
{ | |
$objDomainPath = [ADSI]'' | |
} | |
$objSearcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ArgumentList $objDomainPath | |
} | |
# Setup LDAP filter | |
$objSearcher.PageSize = $Limit | |
$objSearcher.Filter = $LdapFilter | |
$objSearcher.SearchScope = 'Subtree' | |
} | |
Process | |
{ | |
try | |
{ | |
# Return object | |
$objSearcher.FindAll() | ForEach-Object -Process { | |
$_ | |
} | |
} | |
catch | |
{ | |
"Error was $_" | |
$line = $_.InvocationInfo.ScriptLineNumber | |
"Error was in Line $line" | |
} | |
} | |
End | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment