Weekend Scripter: Finding In-Use Phone Extensions

Weekend Scripter: Finding In-Use Phone Extensions

  • Comments 4
  • Likes

Summary: Guest blogger Tim Bolton talks about using Windows PowerShell to determine what phone extensions are currently in use.

Microsoft Scripting Guy, Ed Wilson, is here. It is amazing the number of manual tasks that can be safely eliminated by using a little bit of Windows PowerShell. In fact, it is also amazing, in general, how little time it actually takes to create something useful in Windows PowerShell. The funny thing is that even people who “know PowerShell” still fall into the trap of doing something manually—at least I know that I do!

Yes, I admit it … there are still times when I do things manually. Why? Well, I will admit that at times I am lazy. I have been using a mouse a lot longer than I have been using Windows PowerShell. Still, I will say that with Windows 8 and Windows PowerShell 3.0, those manual excursions are becoming less and less frequent. This is because we have so much more built-in cmdlet (and function) coverage that often it really is easier to use Windows PowerShell than to use the mouse. All of our new products incorporate Windows PowerShell to facilitate management. This is great news for people who just want “to get the job done.”  

Learn Windows PowerShell—it will save you time and effort.

Today, we have another guest blog post written by Tim Bolton. Tim is learning Windows PowerShell. His article is immediately useful, but it’s also an encouragement to others who want to learn this useful technology.

For more about Tim, see his extremely popular Why Should I Learn Powershell? Real World Example Saves the Day! guest Hey, Scripting Guy! Blog post.

Here's Tim ...

Finding In-Use Phone Extensions

After doing this manually by checking an Excel spreadsheet and dialing each number manually, I was once again reminded of the following rule of POSH: If you have to do something more than twice, script it.

In my current position, one of the common daily tasks is to assign telephone numbers to new users and also reassign numbers depending on where they are working within the company. Once again, with the help and inspiration of my coworkers Brian Peacock and Arturo (Art) Carrera, this script came to life.

Initially the script looked something like this. This was not pretty to look at and my coffee would get cold waiting for it to finish.  #FAIL  –

Function Get-Extension {

      param(

      $Extension = (Read-Host "What is the Extension you want to check? ")

            )

      $CSUser=Get-csuser | where {$_.LineURI -like “*$Extension"}

      $GetUser=Get-user | where {$_.Phone -like “*$Extension”"}

      $GetUM=Get-UMMailbox -resultsize unlimited | where {$_.PhoneNumber -like "*$Extension"}

      $obj = New-Object -TypeName PSObject

      $obj | Add-Member -MemberType NoteProperty -Name "CS Display Name" -Value ($CSUser.DisplayName)

      $obj | Add-Member -MemberType NoteProperty -Name "Line URI" -Value ($CSUser.LineURI)

      $obj | Add-Member -MemberType NoteProperty -Name "AD Phone" -Value($GetUser.Phone)

      $obj | Add-Member -MemberType NoteProperty -Name "Exchange SIP" -Value ($GetUM.SIPResourceIdentifier)

      $obj | Add-Member -MemberType NoteProperty -Name "Exchange Extension" -Value ($GetUM.Extensions)

      Write-Output $obj

}

After I found the few -Filters I could actually use, we got it down to about 2 to 3 minutes. Not too bad but GEEZ! This “is” Windows PowerShell, right? So, this is the second generation, but once again, it felt like well … #FAIL  –

Function Get-Extension {

      param(

            $Extension = (Read-Host "What is the Extension you want to check? ")

            )

      $CSUser=Get-csuser -Filter "LineURI -like '*$Extension'"

      $GetUser=Get-user -Filter "Phone -like '*$Extension'"

      $GetUM=Get-UMMailbox -resultsize unlimited | where {$_.PhoneNumber -like "*$Extension"}

      $obj = New-Object -TypeName PSObject     

      $obj | Add-Member -MemberType NoteProperty -Name "CS Display Name" -Value ($CSUser.DisplayName)   

      $obj | Add-Member -MemberType NoteProperty -Name "Line URI" -Value ($CSUser.LineURI)     

      $obj | Add-Member -MemberType NoteProperty -Name "AD Phone" -Value($GetUser.Phone)    

      $obj | Add-Member -MemberType NoteProperty -Name "Exchange SIP" -Value ($GetUM.SIPResourceIdentifier)     

      $obj | Add-Member -MemberType NoteProperty -Name "Exchange Extension" -Value ($GetUM.Extensions)

      Write-Output $obj

}

Then, my coworker Brian mentioned saving it as a string and making it a bit faster instead of waiting for the screen to prompt me for the extension.

This also made it possible (to my surprise) to use  –Filter with Get-UMMailbox, which made it much faster—down to 15 seconds!  #WINNER!  -

Function Get-Extension {

      param(

      [parameter(Mandatory = $true)]

      [String] $Extension)

$CSUser=Get-csuser -Filter "LineURI -like '*$Extension'"

$GetUser=Get-User -Filter "Phone -like '*$Extension'"

$GetOther=Get-User -Filter "OtherFax -Like '*$Extension*'"

$GetUM=Get-UMMailbox -Filter "EmailAddresses -Like '*$Extension*'" # Exchange

$NotFound = "Extension Not Found"

As before, I will answer some of the most common questions I have received about this script to save you some time.

  1. Yes, Yes, Yes, Josh, I know I'm killing puppies with the Write-Host, BUT again, it’s just for visual verification only.
  2. Was there much of a difference in speed gained by using –Filter? YES! We went from waiting anywhere from 4- 6 minutes to 2 -3 minutes to just over 15 seconds. Behold the power of the Shell!
  3. Why use New-Object?  Honestly, Claus, Jeffery H, and Jeff W. I just happened to be on chapter 19 of Learn Windows PowerShell in a Month of Lunches (second edition) and, well, it looked like a good idea.
  4. Why are you worried about the OtherFax Number?  The reason for that, Brian, is that I have found the techs in the past were using Phone Numbers for Fax Numbers and vice versa. This can cause you to go insane trying to figure out why a particular number will not work correctly searching by Phone Number or Extension alone.
  5. Jeffery, I used the [String] setting, per Brian’s suggestion, to exclude the prompt for an extension making it a bit faster, and I soon realized (to my surprise) that I would now be able to use –Filter with Get-UMMailbox, which otherwise was nearly impossible. Why the limitation on the Filters available, I have no idea. That would be a good question to bring up with the Exchange team.
  6. This is yet another reason to actually “Learn” Windows PowerShell. Do not just copy other people’s work. You are doing yourself a disservice. It did not even occur to me to make it a string, but now I am actually starting to “get it.” The more I “Learn” about Windows PowerShell, the more I want to use it, and I am better off for it.

 Link to the repository: http://gallery.technet.microsoft.com/scriptcenter/Get-Extension-a4213c52

~Tim

Thank you, Tim! Great job, and thanks for sharing your script with us today. Join me tomorrow for more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I also threw this in so that you can kick it off and it will tell you once its complete. Just place Done at the end of the script. It will say the first name of whom ever is running it.  Place the function at the top and Done right after

    Write-Output $obj  but before the closing  }

    Function Done()

    {

    $MyName = $env:username

    $Tech=(Get-User $MyName).FirstName

    $Voice = new-object -ComObject SAPI.SpVoice;

    $Voice.Speak("$Tech, Here are your results") | Out-Null

    }

  • I have made some updates to this script which you can read about here http://www.minasi.com/forum/topic.asp?TOPIC_ID=41644