{"id":64353,"date":"2007-07-28T01:04:00","date_gmt":"2007-07-28T01:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/28\/how-can-i-map-a-printer-based-on-the-computers-ou\/"},"modified":"2007-07-28T01:04:00","modified_gmt":"2007-07-28T01:04:00","slug":"how-can-i-map-a-printer-based-on-the-computers-ou","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-map-a-printer-based-on-the-computers-ou\/","title":{"rendered":"How Can I Map a Printer Based on the Computer\u2019s OU?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I map a printer based on the local computer\u2019s OU?<BR><BR>&#8212; TG <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, TG. Before we begin, we\u2019d like to offer a sincere and heartfelt apology to the Scripting Editor. In a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jul07\/hey0724.mspx\"><B>recent column<\/B><\/A>, we made it sound as if, in her words, the Scripting Editor was a \u201cditzy teenager.\u201d 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\u2019re sorry, Scripting Editor; you are <I>not<\/I> a ditzy teenager.<\/P>\n<P>Well, you\u2019re not a teenager, anyway. <\/P>\n<P>OK, now that the Scripting Editor is happy let\u2019s move on to the business at hand: writing a script that can map a printer based in the local computer\u2019s OU. Following our usual <I>modus operandi<\/I> (a Latin phrase meaning \u201cmodus operandi\u201d), let\u2019s take a look at the script, then see if we can explain how it all works.<\/P>\n<TABLE class=\"dataTable\" id=\"ELD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Funny you should mention that, but, yes, Latin <I>was<\/I> the language the Scripting Editor spoke as a girl. But back in those days, that was the language most people spoke.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)\nstrName = objSysInfo.ComputerName<\/p>\n<p>arrComputerName = Split(strName, &#8220;,&#8221;)\narrOU = Split(arrComputerName(1), &#8220;=&#8221;)\nstrComputerOU = arrOU(1)<\/p>\n<p>Set objNetwork = CreateObject(&#8220;WScript.Network&#8221;)<\/p>\n<p>Select Case strComputerOU\n    Case &#8220;Client&#8221;\n        objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer1\\ClientPrinter&#8221;\n        objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer1\\ClientPrinter&#8221;\n    Case &#8220;Finance&#8221;\n        objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer2\\FinancePrinter&#8221;\n        objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer2\\FinancePrinter&#8221;\n    Case &#8220;Human Resources&#8221;\n        objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer3\\HRPrinter&#8221;\n        objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer3\\HRPrinter&#8221;\n    Case &#8220;Research&#8221;\n        objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer4\\ResearchPrinter&#8221;\n        objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer4\\ResearchPrinter&#8221;\n    Case Else\n        objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer5\\GenericPrinter&#8221;\n        objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer5\\GenericPrinter&#8221;\nEnd Select\n<\/PRE>\n<P>As you can see, we start out by creating an instance of the <B>ADSystemInfo <\/B>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 <I>local<\/I> 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.<\/P>\n<P>After creating the ADSystemInfo object we next assign the value of the <B>ComputerName<\/B> property to a variable named strName:<\/P><PRE class=\"codeSample\">strName = objSysInfo.ComputerName\n<\/PRE>\n<P>If you aren\u2019t familiar with the ADSystemInfo object, we should note that the ComputerName property returns the distinguished name of the local computer; that\u2019s going to be a value similar to this one:<\/P><PRE class=\"codeSample\">CN=ATL-WS-01,OU=Research,OU=North America,DC=fabrikam,DC=com\n<\/PRE>\n<P>If you take a moment to look over this value, you\u2019ll see that the OU where the computer account resides is embedded within the distinguished name: the first item in the string contains the computer\u2019s CN (CN=ATL-WS-01) and the second item contains the computer\u2019s OU (OU=Research). That\u2019s great, except for one thing: how do we extract the OU from the distinguished name?<\/P>\n<P>Here\u2019s one way:<\/P><PRE class=\"codeSample\">arrComputerName = Split(strName, &#8220;,&#8221;)\narrOU = Split(arrComputerName(1), &#8220;=&#8221;)\nstrComputerOU = arrOU(1)\n<\/PRE>\n<P>What are we doing here? Well, in the first line we\u2019re using the <B>Split<\/B> function to transform the computer\u2019s distinguished name into an array; by splitting the value on the comma we end up with an array containing the following elements:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>CN=ATL-WS-01<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>OU=Research<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>OU=North America<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>DC=fabrikam<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>DC=com<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>As you can see, the second item in the array (an item which has an index number of 1) is the information we\u2019re looking for. Unfortunately, though, it\u2019s not exactly in the desired format; we\u2019d prefer to have the name Research rather than the value OU=Research. So <I>now<\/I> what do we do?<\/P>\n<P>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\u2019s what our second line of code does:<\/P><PRE class=\"codeSample\">arrOU = Split(arrComputerName(1), &#8220;=&#8221;)\n<\/PRE>\n<P>Upon execution, that\u2019s going to give us another array (an array named arrOU) that includes <I>these<\/I> items:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>OU<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Research<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>And yes, you\u2019re way ahead of us: the second item in this array (the item with an index number of 1) is the very value we\u2019re looking for. Consequently, we assign the value of that item to a variable named strComputerOU:<\/P><PRE class=\"codeSample\">strComputerOU = arrOU(1)\n<\/PRE>\n<P>Now that we know the computer\u2019s OU we can create an instance of the <B>Wscript.Network<\/B> 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\u2019s why we have code similar to this:<\/P><PRE class=\"codeSample\">Select Case strComputerOU\n    Case &#8220;Client&#8221;\n        objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer1\\ClientPrinter&#8221;\n        objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer1\\ClientPrinter&#8221;\n<\/PRE>\n<P>This block of code should be self-explanatory. If the value of strComputerOU is equal to <I>Client<\/I> (that is, if the computer account resides in the Client OU) we use the <B>AddWindowsPrinterConnection<\/B> method to map a printer to \\\\PrinterServer1\\ClientPrinter; once the printer has been added we then call the <B>SetDefaultPrinter<\/B> method to make this the default printer. <\/P>\n<P>Ah, but what if the computer name <I>isn\u2019t<\/I> equal to <I>Client<\/I>? That\u2019s fine; we simply check the next value in the Select Case block. If the OU name can\u2019t be found at all, then we default to the <B>Case Else<\/B> section and map a \u201cgeneric\u201d printer instead:<\/P><PRE class=\"codeSample\">Case Else\n    objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer5\\GenericPrinter&#8221;\n    objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer5\\GenericPrinter&#8221;\n<\/PRE>\n<P>Believe it or not, that\u2019s all we have to do.<\/P>\n<P>We hope that gets you started, TG. In the meantime, we\u2019d 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\u2019ve decided to go along with that: the Scripting Guy who writes this column will wash the Scripting Editor\u2019s car the next time he washes his own car, and he will clean her office the next he cleans <I>his<\/I> office. Based on current trends, she can expect to get her car washed sometime in early 2013. As for cleaning the office, well, let\u2019s put it this way: let\u2019s hope that she eats right and gets plenty of exercise. Otherwise, she probably won\u2019t be around long enough to experience a newly-cleaned office.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I map a printer based on the local computer\u2019s OU?&#8212; TG Hey, TG. Before we begin, we\u2019d 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 \u201cditzy teenager.\u201d That, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,445,43,404,3,5],"class_list":["post-64353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-client-side-printing","tag-ous","tag-printing","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I map a printer based on the local computer\u2019s OU?&#8212; TG Hey, TG. Before we begin, we\u2019d 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 \u201cditzy teenager.\u201d That, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64353"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64353\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}