This is the first version of a script that uses .NET to query Active Directory. Some limitations apply at the moment, such as a proper filtering system, but that will hopefully be added in future updates.
# Get-ADObject.ps1
# Version 1.0
#Requires -Version 2
<#
.SYNOPSIS
Get object from Active Directory
.PARAMETER ObjectName
The name of the object you want to retrieve
.PARAMETER ObjectType
The type of object you want to retrieve (examples: user,computer,group)
.PARAMETER Domain
Active Directory domain
.EXAMPLE
Get-ADObject Server001 -ObjectType Computer -Domain my.domain.com
Get computer object for Server001
.EXAMPLE
Get-ADObject server* -Domain my.domain.com
Get all objects that starts with server
.EXAMPLE
Get-ADObject * -ObjectType user
Get all user objects in the current domain
.DESCRIPTION
This script will get object data from Active Directory
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
[String]$ObjectName,
[String]$ObjectType = "*",
[String]$Domain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Name
)
BEGIN{
# Build LDAP string for AD root
$domainSplit = $domain.Split(".")
$adRootLDAP = "LDAP://"
foreach ($split in $domainSplit){$adRootLDAP_tmp += ",DC=$($split)"}
$adRootLDAP += $adRootLDAP_tmp.TrimStart(",")
# Define AD Root
$adRoot = New-Object System.DirectoryServices.DirectoryEntry($adRootLDAP)
}
PROCESS{
# Build LDAP Search Filter
$adFilter = "(&(objectClass=$($ObjectType)) (Name=$ObjectName))"
# Set up AD Searcher
$adSearcher = New-Object System.DirectoryServices.DirectorySearcher($adRoot)
$adSearcher.Filter = $adFilter
# Perform search
$adSearchResult = $adSearcher.FindAll()
# Output object
Write-Output $adSearchResult
}
END{$adSearchResult.Dispose()}