Share via


HOW TO CREATE A FUNCTION TO VALIDATE THE EXISTENCE OF AN AD OBJECT (Test-XADObject)

In this posting I am sharing a simple but useful function that you might need to use in many of your scripts. This is a function that returns true if a given AD object exists provided its identity. The Identity parameter can receive any ADObject identity. This means that it can receive identities that *-ADObject cmdlets can receive.

    1:  function Test-XADObject() {
    2:   
    3:     [CmdletBinding(ConfirmImpact="Low")]
    4:   
    5:     Param (
    6:   
    7:        [Parameter(Mandatory=$true,
    8:   
    9:                   Position=0,
   10:   
   11:                   ValueFromPipeline=$true,
   12:   
   13:                   HelpMessage="Identity of the AD object to verify if exists or not."
   14:   
   15:                  )]
   16:   
   17:        [Object] $Identity
   18:   
   19:     )
   20:   
   21:     trap [Exception] {
   22:   
   23:        return $false
   24:   
   25:     }
   26:   
   27:     $auxObject = Get-ADObject -Identity $Identity
   28:   
   29:     return $true
   30:   
   31:  }

In a future posting I will share an example of a script that uses a couple of times this function. Stay tuned.

Cheers,

Jairo Cadena

Active Directory

Comments