How Can I Map a Printer Based on the Computer’s OU?

How Can I Map a Printer Based on the Computer’s OU?

  • Comments 5
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I map a printer based on the local computer’s OU?

-- TG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TG. Before we begin, we’d like to offer a sincere and heartfelt apology to the Scripting Editor. In a recent column, we made it sound as if, in her words, the Scripting Editor was a “ditzy teenager.” That, of course, is not the case. Instead, we were just poking a little good-natured fun at our beloved Scripting Editor. In addition, we assumed that, seeing as how she is well into her twilight years, the Scripting Editor would be flattered to know that someone thought of her as being young, vibrant, and alive. That was an assumption we should not have made without asking, and we apologize for that. In other words, we’re sorry, Scripting Editor; you are not a ditzy teenager.

Well, you’re not a teenager, anyway.

OK, now that the Scripting Editor is happy let’s move on to the business at hand: writing a script that can map a printer based in the local computer’s OU. Following our usual modus operandi (a Latin phrase meaning “modus operandi”), let’s take a look at the script, then see if we can explain how it all works.

Note. Funny you should mention that, but, yes, Latin was the language the Scripting Editor spoke as a girl. But back in those days, that was the language most people spoke.

Here’s the script:

Set objSysInfo = CreateObject("ADSystemInfo")
strName = objSysInfo.ComputerName

arrComputerName = Split(strName, ",")
arrOU = Split(arrComputerName(1), "=")
strComputerOU = arrOU(1)

Set objNetwork = CreateObject("WScript.Network")

Select Case strComputerOU
    Case "Client"
        objNetwork.AddWindowsPrinterConnection "\\PrintServer1\ClientPrinter"
        objNetwork.SetDefaultPrinter "\\PrintServer1\ClientPrinter"
    Case "Finance"
        objNetwork.AddWindowsPrinterConnection "\\PrintServer2\FinancePrinter"
        objNetwork.SetDefaultPrinter "\\PrintServer2\FinancePrinter"
    Case "Human Resources"
        objNetwork.AddWindowsPrinterConnection "\\PrintServer3\HRPrinter"
        objNetwork.SetDefaultPrinter "\\PrintServer3\HRPrinter"
    Case "Research"
        objNetwork.AddWindowsPrinterConnection "\\PrintServer4\ResearchPrinter"
        objNetwork.SetDefaultPrinter "\\PrintServer4\ResearchPrinter"
    Case Else
        objNetwork.AddWindowsPrinterConnection "\\PrintServer5\GenericPrinter"
        objNetwork.SetDefaultPrinter "\\PrintServer5\GenericPrinter"
End Select

As you can see, we start out by creating an instance of the ADSystemInfo object, an object that returns a lot of useful information about the logged-on user, his or her domain, and the local computer. And yes, we are talking the local computer; the ADSystemInfo object cannot be created on a remote computer. That means the preceding script can be run only on the local machine. Which, among other things, makes the code a good candidate for inclusion in a logon script.

After creating the ADSystemInfo object we next assign the value of the ComputerName property to a variable named strName:

strName = objSysInfo.ComputerName

If you aren’t familiar with the ADSystemInfo object, we should note that the ComputerName property returns the distinguished name of the local computer; that’s going to be a value similar to this one:

CN=ATL-WS-01,OU=Research,OU=North America,DC=fabrikam,DC=com

If you take a moment to look over this value, you’ll see that the OU where the computer account resides is embedded within the distinguished name: the first item in the string contains the computer’s CN (CN=ATL-WS-01) and the second item contains the computer’s OU (OU=Research). That’s great, except for one thing: how do we extract the OU from the distinguished name?

Here’s one way:

arrComputerName = Split(strName, ",")
arrOU = Split(arrComputerName(1), "=")
strComputerOU = arrOU(1)

What are we doing here? Well, in the first line we’re using the Split function to transform the computer’s distinguished name into an array; by splitting the value on the comma we end up with an array containing the following elements:

CN=ATL-WS-01

OU=Research

OU=North America

DC=fabrikam

DC=com

As you can see, the second item in the array (an item which has an index number of 1) is the information we’re looking for. Unfortunately, though, it’s not exactly in the desired format; we’d prefer to have the name Research rather than the value OU=Research. So now what do we do?

Well, what we do now is use the Split function once more, this time to split the value of array item 1, and this time using the equals sign as the character to split on. That’s what our second line of code does:

arrOU = Split(arrComputerName(1), "=")

Upon execution, that’s going to give us another array (an array named arrOU) that includes these items:

OU

Research

And yes, you’re way ahead of us: the second item in this array (the item with an index number of 1) is the very value we’re looking for. Consequently, we assign the value of that item to a variable named strComputerOU:

strComputerOU = arrOU(1)

Now that we know the computer’s OU we can create an instance of the Wscript.Network object (used for mapping printers); after that we can then set up a Select Case statement and map a printer based on the OU name. That’s why we have code similar to this:

Select Case strComputerOU
    Case "Client"
        objNetwork.AddWindowsPrinterConnection "\\PrintServer1\ClientPrinter"
        objNetwork.SetDefaultPrinter "\\PrintServer1\ClientPrinter"

This block of code should be self-explanatory. If the value of strComputerOU is equal to Client (that is, if the computer account resides in the Client OU) we use the AddWindowsPrinterConnection method to map a printer to \\PrinterServer1\ClientPrinter; once the printer has been added we then call the SetDefaultPrinter method to make this the default printer.

Ah, but what if the computer name isn’t equal to Client? That’s fine; we simply check the next value in the Select Case block. If the OU name can’t be found at all, then we default to the Case Else section and map a “generic” printer instead:

Case Else
    objNetwork.AddWindowsPrinterConnection "\\PrintServer5\GenericPrinter"
    objNetwork.SetDefaultPrinter "\\PrintServer5\GenericPrinter"

Believe it or not, that’s all we have to do.

We hope that gets you started, TG. In the meantime, we’d like to apologize once more to the Scripting Editor. As part of his penance, the Scripting Editor suggested that the Scripting Guy who writes this column wash her car and clean her office. As a gesture of good faith, we’ve decided to go along with that: the Scripting Guy who writes this column will wash the Scripting Editor’s car the next time he washes his own car, and he will clean her office the next he cleans his office. Based on current trends, she can expect to get her car washed sometime in early 2013. As for cleaning the office, well, let’s put it this way: let’s hope that she eats right and gets plenty of exercise. Otherwise, she probably won’t be around long enough to experience a newly-cleaned office.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks a billion, trillion,... for this script... it has been exactly what I was looking for.  But don't forget you have to "Dim" your variables first...!!!

  • You are correct, and personally I always dim my variables in my personal scripts. However, for Hey Scripting Guy! articles, the collection of DIM statements adds an awful lot of length to the script code sections, and that is why we leave them off. In addition, we generally leave most error handling and comments out of the code as well. This is also due to the wishing to focus only on the specific task at hand.

  • We have tried the above method of printer mapping via GPO using script within our NETLOGON area. We have pretty much followed your suggestions word for word, but are struggling with a black screen issue after the user logs on. The time of this black screen seems to range on various machines from 2mins - 10mins. Initially we thought it might be the drivers we had installed on our print server, but after changing these the problem still persists. Any advice would be much appreciated.

  • @Sean -

    Yes - do not use a logon script to map printers. Use Group Policy preferences.

  • thanks