Public/Get-ADSIObject.ps1

function Get-ADSIObject
{
<#
.SYNOPSIS
    This function will query any kind of object in Active Directory
 
.DESCRIPTION
    This function will query any kind of object in Active Directory
 
.PARAMETER SamAccountName
    Specify the SamAccountName of the object.
    This parameter also search in Name and DisplayName properties
    Name and Displayname are alias.
 
.PARAMETER DistinguishedName
    Specify the DistinguishedName of the object your are looking for
     
.PARAMETER Credential
    Specify the Credential to use
     
.PARAMETER $DomainDistinguishedName
    Specify the DistinguishedName of the Domain to query
     
.PARAMETER SizeLimit
    Specify the number of item(s) to output
     
.EXAMPLE
    Get-ADSIObject -SamAccountName Fxcat
 
.EXAMPLE
    Get-ADSIObject -Name DC*
     
.NOTES
    Francois-Xavier Cat
    LazyWinAdmin.com
    @lazywinadm
#>

    
    [CmdletBinding()]
    PARAM (
        [Parameter(ParameterSetName = "SamAccountName")]
        [Alias("Name", "DisplayName")]
        [String]$SamAccountName,
        
        [Parameter(ParameterSetName = "DistinguishedName")]
        [String]$DistinguishedName,
        
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [Alias("Domain", "DomainDN", "SearchRoot", "SearchBase")]
        [String]$DomainDistinguishedName = $(([adsisearcher]"").Searchroot.path),
        
        [Alias("RunAs")]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty,
        
        [Alias("ResultLimit", "Limit")]
        [int]$SizeLimit = '100'
    )
    BEGIN { }
    PROCESS
    {
        TRY
        {
            # Building the basic search object with some parameters
            $Search = New-Object -TypeName System.DirectoryServices.DirectorySearcher -ErrorAction 'Stop'
            $Search.SizeLimit = $SizeLimit
            $Search.SearchRoot = $DomainDistinguishedName
            
            IF ($PSBoundParameters['SamAccountName'])
            {
                $Search.filter = "(|(name=$SamAccountName)(samaccountname=$SamAccountName)(displayname=$samaccountname))"
            }
            IF ($PSBoundParameters['DistinguishedName'])
            {
                $Search.filter = "(&(distinguishedname=$DistinguishedName))"
            }
            IF ($PSBoundParameters['DomainDistinguishedName'])
            {
                IF ($DomainDistinguishedName -notlike "LDAP://*") { $DomainDistinguishedName = "LDAP://$DomainDistinguishedName" }#IF
                Write-Verbose -Message "Different Domain specified: $DomainDistinguishedName"
                $Search.SearchRoot = $DomainDistinguishedName
            }
            IF ($PSBoundParameters['Credential'])
            {
                $Cred = New-Object -TypeName System.DirectoryServices.DirectoryEntry -ArgumentList $DomainDistinguishedName, $($Credential.UserName), $($Credential.GetNetworkCredential().password)
                $Search.SearchRoot = $Cred
            }
            
            foreach ($Object in $($Search.FindAll()))
            {
                # Define the properties
                # The properties need to be lowercase!!!!!!!!
                $Properties = @{
                    "DisplayName" = $Object.properties.displayname -as [string]
                    "Name" = $Object.properties.name -as [string]
                    "ObjectCategory" = $Object.properties.objectcategory -as [string]
                    "ObjectClass" = $Object.properties.objectclass -as [string]
                    "SamAccountName" = $Object.properties.samaccountname -as [string]
                    "Description" = $Object.properties.description -as [string]
                    "DistinguishedName" = $Object.properties.distinguishedname -as [string]
                    "ADsPath" = $Object.properties.adspath -as [string]
                    "LastLogon" = $Object.properties.lastlogon -as [string]
                    "WhenCreated" = $Object.properties.whencreated -as [string]
                    "WhenChanged" = $Object.properties.whenchanged -as [string]
                }
                
                # Output the info
                New-Object -TypeName PSObject -Property $Properties
            }
        }
        CATCH
        {
            Write-Warning -Message "[PROCESS] Something wrong happened!"
            Write-Warning -Message $error[0].Exception.Message
        }
    }
    END
    {
        Write-Verbose -Message "[END] Function Get-ADSIObject End."
    }
}