Freigeben über


Sample Powershell Active Directory Phonebook Script

I have been learning the syntax of Powershell so I thought I would write a simple little script to search Active Directory for the mobile phone numbers of my co-workers.  This script will accept an input of lastname and even take wildcard characters so if I wanted to search for all people's mobile phone numbers that start with min I can just use min* as my search string.

Check it out:

function getmobilenumber($name)
{
  $root = New-Object DirectoryServices.DirectoryEntry
  $Searcher = New-Object DirectoryServices.DirectorySearcher
  $Searcher.Searchroot = $root
  $searcher.filter = ("(sn=$name)")
  $Proplist = "cn","mobile"
  $Proplist | foreach {[void]$Searcher.Propertiestoload.Add($_)}
  $results = $searcher.findall()
  foreach ($result in $results){$result.properties}
}

To use it just type getmobilephonenumber("lastname") where lastname is the name of the person you are searching for.  Note that the $proplist variable could be changed to whatever AD attributes that you wanted to search for.

Enjoy!

BTW I learned a lot on how to do this from this blog entry: https://mow001.blogspot.com/2006/04/large-ad-queries-in-monad.html

Comments

  • Anonymous
    January 01, 2003
    Tim posted another great sample script on his blog
    function getmobilenumber($name){  $root = New-Object...
  • Anonymous
    June 26, 2006
    Nifty. Mind if I post this to the script center? http://www.microsoft.com/technet/scriptcenter/csc/default.mspx
  • Anonymous
    June 26, 2006
    Not at all.  Go for it.  :)
  • Anonymous
    June 27, 2006
    Glad my blogentry was of help.

    Greetings //o//
  • Anonymous
    July 05, 2006
    Great little script. I have done something similar here but have used an Ambiguous Name Resolution Search to allow for a more attributes to be searched against, just changing the search filter to ("(ANR=$name)") will do it. Just hits AD a bit harder is the only issue.