Book Image

Active Directory with PowerShell

By : Pamarthi Venkata Sitaram, YELLAPRAGADA U PADMAVATHI
5 (1)
Book Image

Active Directory with PowerShell

5 (1)
By: Pamarthi Venkata Sitaram, YELLAPRAGADA U PADMAVATHI

Overview of this book

Table of Contents (16 chapters)
Active Directory with PowerShell
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Checking whether a user, group, computer, or an OU exists


Here is a simple script to check whether a user, computer, group, or OU exists in Active Directory with a given name. This script is capable of accepting multiple names and testing to see whether they exist:

Function Test-ADObject {
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$true,Position=0)]
  [string[]]$Name,
  [Parameter(Mandatory=$true,Position=1)]
  [ValidateSet("User","Group","Computer","organizationalUnit")]
  [string]$Type
)
foreach($ObjName in $Name) {
  $OutputObj = New-Object -TypeName PSObject -Property @{
        Name = $ObjName;
        IsFound = $null;
        ObjectClass = $Type }
  try {
    $ObjOut = @(Get-ADObject -Filter { Name -eq $ObjName -and  ObjectClass -eq $Type } -EA Stop)

    if($ObjOut.count -eq 0) {
      $OutputObj.IsFound = $false
    }

    if($ObjOut.Count -gt 1) {
      $OutputObj.IsFound = $false
      Write-Verbose "Multiple objects found with the name  $ObjName"
    }

    if($ObjOut.Count...