PowerShell Script: Finding A Distinguished Name of a Group/User: Function Find-DN

Posted on March 8, 2010. Filed under: Uncategorized |

By: Brenton Blawat

This article explains the the method by which one would be able to Search Active Directory for the distinguished name of a User or Group. This is helpful when trying to add an object to Active Directory or adding Users to Groups.

Function Find-DN Finding Distinguished Name: Download PS1 Here

   1: # Function Find Distinguished Name

   2: function find-dn { param([string]$adfindtype, [string]$cName)

   3:     # Create A New ADSI Call

   4:     $root = [ADSI]''

   5:     # Create a New DirectorySearcher Object

   6:     $searcher = new-object System.DirectoryServices.DirectorySearcher($root)

   7:     # Set the filter to search for a specific CNAME

   8:     $searcher.filter = "(&(objectClass=$adfindtype) (CN=$cName))"

   9:     # Set results in $adfind variable

  10:     $adfind = $searcher.findall()

  11:     

  12:     # If Search has Multiple Answers 

  13:     if ($adfind.count -gt 1) {

  14:         $count = 0 

  15:         foreach($i in $adfind)

  16:         {

  17:             # Write Answers On Screen

  18:             write-host $count ": " $i.path

  19:             $count += 1

  20:         }

  21:         # Prompt User For Selection

  22:         $selection = Read-Host "Please select item: "

  23:         # Return the Selection

  24:         return $adfind[$selection].path

  25:     }

  26:     # Return The Answer

  27:     return $adfind[0].path

  28: }

Using this Function:

   1: # To use the function to find a User:

   2: find-dn "user" "Blawat"

   3:  

   4: # To use the function to find a Group:

   5: find-dn "group" "IT Help Desk"


Read Full Post | Make a Comment ( None so far )

Recently on Business and Information Technology Tangents...

2010 Volkswagen CC Review – A test Drive Bashing

Posted on February 23, 2010. Filed under: Car Tangents |

PowerShell Script: Retrieving Distinguished name (DN) from A Fully Qualified Domain Name (FQDN)

Posted on February 22, 2010. Filed under: Powershell Tangents |

PowerShell Script: Distinguished Name / Fully Qualified Domain Name to string

Posted on February 22, 2010. Filed under: Powershell Tangents |

Default Domain Policies Windows Server 2003 SP2 / Windows server 2008 R2

Posted on February 3, 2010. Filed under: Server Tangents |

2008 BMW 335XI – Fuel Delivery Lemon Story (Part 2)

Posted on February 1, 2010. Filed under: Car Tangents |

Basic How to Guide for the Symbol (Motorola) PPT-8846

Posted on February 3, 2009. Filed under: Hardware Tangents |

2008 BMW 335XI – Fuel Delivery Lemon Story

Posted on January 24, 2009. Filed under: Car Tangents |

Wavelink Enabler Issues – A story of the Subpar Enablers

Posted on January 23, 2009. Filed under: Software Tangents |

Blackberry Touch / Storm Review: Still no iPhone

Posted on November 20, 2008. Filed under: Product Review Tangents |

Building the Bad Ass Development Rig

Posted on October 30, 2008. Filed under: Hardware Tangents |

    About

    Business and Information Technology Tangents is dedicated to providing quality content while informing the world about technology.

    RSS

    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET News.com
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS
    • Subscribe in Rojo

    Meta

Liked it here?
Why not try sites on the blogroll...