Get-ObjectAttribs (get Active Directory object attributes function)

Not dependent on Quest AD cmdlets. Returns an object with all of the attributes of the AD object — pipe the results to fl to see.

function Get-ObjectAttribs {
  <#   .SYNOPSIS   Retrieves any Active Directory object with all atrributes from samaccountname or distinguishedname input. :: by Kurt Bestul - kab@bestul.us   .DESCRIPTION   Retrieves any Active Directory object with all atrributes from samaccountname or distinguishedname input. Use when processing a collection of objects from a subtree, where the object may be either a group or a user; then you can check the objectcategory to determine how to branch in your code.   .EXAMPLE   $objTargetObject = Get-ObjectAttribs -id someobject   $strObjectType = [string] $objTargetObject.objectcategory   .PARAMETER id   The object samaccountname to process. Only one value per call.   .PARAMETER idtype   Accepts 0 or 1, where default is 1. If 0, the distinguishedname is used for id filter. If 1, samaccountname is used.   #>
  [CmdletBinding()]
  param
  (
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [ValidateLength(3,255)]
        [string]$id,
        [parameter(Mandatory=$false)]
        [Int]
        [ValidateRange(0,2)]
        $idtype = 1 #setting 1 as default value
  )
  process {
      $oDom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
      $oRoot = [ADSI] "LDAP://$($oDom.Name)"
      $oSearcher = New-Object System.DirectoryServices.DirectorySearcher $oRoot
      switch ($idtype)
      { 
      0 {$oSearcher.filter = "((distinguishedname=$id))"}
      1 {$oSearcher.filter = "((samaccountname=$id))"}
      2 {$oSearcher.filter = "((identity=$id))"}
      }

      $sTarget = $oSearcher.FindOne()
      [System.Collections.Arraylist]$oNames = $sTarget.Properties.PropertyNames
      [System.Collections.Arraylist]$oProps = $sTarget.Properties.Values
      $oTargetObj = New-Object System.Object
      for ($i = 0; $i -lt $oNames.Count)
         {
           $oTargetObj | Add-Member -type NoteProperty -Name $($oNames[$i]) -Value $($oProps[$i])
           $i++
         }
      return $oTargetObj
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s