Learn about Windows PowerShell
Our phone numbers are stored in Active Directory as extension numbers; however, we have a set of rules for converting those extensions to actual phone numbers. I’m writing a script to create an Outlook signature, but in that script I need to be able to convert an extension to a phone number. How do I do that?-- SM
Hey, SM. You know, you are looking at one tired Scripting Guy today; in fact, you’re looking at one totally exhausted Scripting Guy. The Scripting Guys have been busily preparing for their forthcoming trip to Barcelona (for the TechEd IT Forum), and the Scripting Guy who writes this column has been working pretty much round the clock the past few days to ensure that he made all his deadlines. He did, although it wasn’t easy, and the past few nights – when he’s averaged maybe 4 hours of sleep – have finally caught up to him.
Note. Didn’t this Scripting Guy know well in advance that he was going to Barcelona? Sure. And hasn’t he had over a month to complete his assignments, meaning he could have done this a long time ago without having to lose a minute of sleep? Yes, of course he did. But what’s your point?
At any rate, the Scripting Guy who writes this column is tired. Too tired to write a new column, even one that shows you how you can convert an extension number to an actual phone number? Yes, way too tired. Come back tomorrow and we’ll see if he feels like writing a new column then.
Nah, just kidding. The Scripting Guy who writes this column always has room for Jell-O, and he always has enough get-up-and-go to write a column that shows you how you can convert an extension number to an actual phone number (hey, who doesn’t?). Here’s a portion of the script; we’ll show you the rest in due time:
strNumber = "7596" If Left(strNumber, 2) = "74" Then strNumber = "3112-74" & Right(strNumber, 2)End If If Left(strNumber, 2) = "75" Then strNumber = "3115-85" & Right(strNumber, 2)End If Wscript.Echo strNumber
strNumber = "7596"
If Left(strNumber, 2) = "74" Then
strNumber = "3112-74" & Right(strNumber, 2)
If Left(strNumber, 2) = "75" Then
strNumber = "3115-85" & Right(strNumber, 2)
Gee, now that we did all that typing we don’t seem to have near enough energy to explain how this script works. Guess we’ll just have to go home then. See you all tomorr –
Never mind; the Scripting Editor just gave us “The Look.”
Note. What’s The Look? Remember in those old science fiction movies when someone would get hit with a blast from a ray gun and would literally explode? Those guys got off easy compared to anyone who’s ever gotten The Look.
Is that our spleen lying over there or has someone else gotten The Look recently?
Anyway, as we were saying, let’s take some time to explain how this script works. In order to keep things simple, all we’re showing you at the moment is code that converts an extension to an actual phone number; at the end of this column, we’ll show you a more complete script, one that can grab the extension from Active Directory, convert the extension to a phone number, and then create the Outlook signature. But all that comes at the end of the column. Here, in the middle of the column, we’re going to limit ourselves to discussing the process by which we convert an extension to a phone number.
Before we do that, however, let’s briefly explain SM’s rules for converting an extension to a phone number. In SM’s organization, extensions have one of two prefixes: 74 or 75. How do you convert an extension like 74 to an actual phone number? Well, suppose we have the extension 7439. In this case, we first convert the 74 (the first two digits in the extension) to 3112-74. We then grab the last two digits of the extension (39) and tack those onto the 3112-74. What does 3112-74 plus 39 give us? That’s right: a valid phone number.
Note. Yes, it would be better if we weren’t acting coy and simply showed you the resulting phone number. For better or worse, however, there are a whole bunch of rules regarding phone numbers and whether a number can or cannot be displayed on the Web. We’re already tired enough as it is, so we decided to bypass the issue altogether. Originally we were going to convert each extension to 555-; that way we could show you a phone number like 555-7439. But it seemed weird to convert the 74 extensions to 555- and to convert the 75 extensions to 555-. So we decided to instead write a column about phone numbers that never once displays a phone number. Another Scripting Guys first!
As for extensions beginning with 75, we do the same sort of thing, in that case combining 3115-75 and the last two digits of the extension.
Which, interestingly enough, is the same process the Scripting Guy who writes this column used to pick out the name of his son. (Why else would we always refer to him as “The Scripting Son” rather than by name?)
As for the script (or script snippet), we start off by assigning an extension (in this case 7596) to a variable named strNumber. We then use the Left function to determine whether or not the first two digits in the extension are equal to 74. If they are, we then construct a new phone number, combining the standard prefix 3112-74 with the last two digits of the extension. And how do we know what the last two digits of the extension are? That’s easy: we simply use the Right function to grab the last two characters in the variable strNumber.
That, by the way, is what this block of code is for:
If Left(strNumber, 2) = "74" Then strNumber = "3112-74" & Right(strNumber, 2)End If
What if the first two digits in the extension aren’t 74? No problem; in that case we just drop down to the next If-Then statement, one that checks to see if the first two digits are equal to 75 and, if so, takes the appropriate action:
If Left(strNumber, 2) = "75" Then strNumber = "3115-85" & Right(strNumber, 2)End If
And, of course, if we had more extension types (say, 76 and 77) we could continue to add additional If-Then blocks as needed.
That’s really all there is to it; when we’re all done we echo back the value of strNumber. To see that value, just close your eyes and imagine a phone number being echoed back to the command window.
Note. By a remarkable coincidence, imaging a phone number being echoed back to the command window is the exact same dream that the Scripting Editor has almost every night. Amazing but true!
Now, how would we use this code snippet in a real, live script? Well, here’s one way:
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
Set objSysInfo = CreateObject("ADSystemInfo")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
strName = objUser.FullName
strTitle = objUser.Title
strDepartment = objUser.Department
strCompany = objUser.Company
strNumber = objUser.telephoneNumber
If Left(strNumber, 2) = "74" Then
strNumber = "3115-85" & Right(strNumber, 2)
Set objEmailOptions = objWord.EmailOptions
Set objSignatureObject = objEmailOptions.EmailSignature
Set objSignatureEntries = objSignatureObject.EmailSignatureEntries
objSelection.TypeText strName & ", " & strTitle
objSelection = objDoc.Range()
objSignatureEntries.Add "AD Signature", objSelection
objDoc.Saved = True
We aren’t going to explain how this script works; we have an entire TechNet Magazine article devoted to that subject. Suffice to say that this script connects to Active Directory and, for the logged-on user, retrieves information such as full name, title, department, company, and, of course, phone number. After converting the extension number to an actual phone number (look for our code snippet … hey, there it is!), the script then proceeds to create a new email signature named AD Signature.
Incidentally, this all happens without anything appearing on screen. If you prefer to watch the magic unfold, make this the second line of code (and comment out the very last line, which causes Word to terminate):
objWord.Visible = True
So now that all the work for Barcelona is done (well, mostly done) what’s next for the Scripting Guys? That would probably be the 2008 Winter Scripting Games, scheduled for February 15, 2008 through March 3, 2008. This time around the Games are going to be way bigger and way better than ever. (For one thing, we’ve added a new language to the mix: Perl.) And yes, that is going to be a lot of work.
But hey, we have plenty of time between now and then, so ….