Question

Calling all VBS scripters....

Asked by: jamesreddy

I'm still a novice vbs script maker, so I'll need an assist with this one.  I want to develop a script that will automatically connect to a network printer based on the computer name that the user logs into.  Basic scenario:

I have several computers labs.  The lab PCs in the different rooms all have computer names that start with the appropriate room number.  All of my PCs names start with one of the following:

NH1   -- Nelson Hall Room 1
NH3   -- Nelson Hall Room 3
NH5   -- Nelson Hall Room 5
DCA   -- Data Center Room A
DC25 -- Data Center Room 25
DCRR -- Data Center Resource Room

Each room has their own network printer.  NH1, NH3, NH5, DCA, DC25 all connect to HPs with JetDirects.  DCRR connects to a shared printer based off an XP Professional machine in the same room.

I need a script for my users (who roam throughout the network) that can determine the PC's name, then connect to the appropriate printer based on that computer name.  I know I've seen similar scripts in EE before, but I'm at a loss to find one again.

Thanks in advance...and be gentle.  :)  Programming is the ONE THING, I've resisted and I'm beginning to subscribe to the Borg philosophy that resistance is futile.

James

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-03-29 at 19:45:45ID20936798
Tags

vbs

Topic

Windows Networking

Participating Experts
3
Points
500
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Certificate Maker
    How do I install the Certificate Maker program on Win 98? It worked fine on 3.1, but doesn't work on 98. It is an old Springboard program--1986.
  2. Resistance vs. Impedance
    Can someone tell me the difference between Resistance vs. Impedance ?
  3. Single class instance throughout Web site...
    i asked this question before but still cannot get it to work... here is what i need: - i have a class written in VB that does something... it has functions/parameters etc. - i need to instance this class in an ASP page once AND ONLY ONCE - i need this instanced class to be ...
  4. VB resist termination
    How does visual basic handle atexit (atexit is in c?) command? I have a program that basically should resist being terminated by any other running processes, in other words, it should always run, it should never be exited. (like a daemon thing) can you give me some few pointe...
  5. jetdirect 175x models
    I need a network print server for the 6110xi hp multi function. It seems the 175x jetdirect works. I saw 6035A on the internet in google. On ebay the 175x jetdirect sells as the 6035a 6035b 6035d 6035g .... does it matter? Do you think this is the best print server for t...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: visioneerPosted on 2004-03-29 at 20:25:06ID: 10710502

You don't need VB to do this.  You can do most of it via command-line scripting.

First of all, look at this thread:
http://www.experts-exchange.com/Operating_Systems/Windows_Server_2003/Q_20936760.html

Create your scripts using Kixtart based on the information I posted in that thread.

In your batch file, use if/then statements based on computername.  For example:

if (%computername%)==(NH1) goto NH1-PC
if (%computername%)==(NH3) goto NH3-PC
goto end

:NH1-PC
<call the script for mapping to the printer in Nelson Hall Room 1>
goto end

:NH3-PC
<call the script for mapping to the printer in Nelson Hall Room 3>
goto end

:end

Let me know if you need clarification, or if you want me to write the darn thing for ya.  :)

 

by: jamesreddyPosted on 2004-03-29 at 20:37:50ID: 10710555

That's a good fall back for me, but the problem with doing command line scripting is that I have the command prompt disabled for security reasons, so users would not be able to open up a command prompt, even for logon scripts.  VB scripts still run without command line options.  Also, my users already have VB logon scripts specified in their logon script.  I guess I could run the whole batch file as a machine script, if I assign everyone print permission, it should be able to connect successfully.

Hrm...I'll have to look into that further...it might be possible.

 

by: jamesreddyPosted on 2004-03-29 at 20:40:02ID: 10710566

Oh...and the other problem with that is that that NH1 is only to first part of the computer name.  Will that work with partial names?  See...NH1 is just the prefix on each computer name.  I have an NH1-PC01, and NH1-PC02, up to NH1-PC24...and similar situations in each lab.  Does that method support just looking at the beginning of the computer name?

 

by: followingPosted on 2004-03-30 at 13:44:26ID: 10718243

Try this for starters:

'*************************************************************
Option Explicit
'On Error Resume Next

Dim WshNetwork, WshShell, sSchool, oWMI, colNetworkPrinters, oPrinter
Dim sComputerName, sRoom, sPrinterPath

'Collect logon info (domain, username, profile path, computername, OS, room, etc.)

Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
sComputerName = WshNetwork.ComputerName
sRoom = Left(sComputerName, 3)

'Clean up previous network printer settings from user's profile

Set colNetworkPrinters = oWMI.ExecQuery ("Select Name from Win32_PrinterConfiguration")
For Each oPrinter in colNetworkPrinters
    If Left(oPrinter.Name, 2) = "\\" Then
        MsgBox ("Deleting printer: " & oPrinter.Name)
      WshNetwork.RemovePrinterConnection oPrinter.Name, True, True
    End If
Next

Select Case sRoom

    Case "NH1"

      sPrinterPath = "\\Server\PrinterShareName"
      WshNetwork.AddWindowsPrinterConnection sPrinterPath
      WshNetwork.SetDefaultPrinter sPrinterPath

    Case "DC2"

      sPrinterPath = "\\Server\PrinterShareName"
      WshNetwork.AddWindowsPrinterConnection sPrinterPath
      WshNetwork.SetDefaultPrinter sPrinterPath


End Select
'***************************************************

Hope this helps,
-jdm

 

by: followingPosted on 2004-03-30 at 13:46:50ID: 10718266

Add more cases for each room.  Might want to add a Case Else to catch anything that doesn't fit anywhere else.  You may be interested in using the printui.dll with rundll32.  At a command prompt, type rundll32 printui.dll,PrintUIEntry /?  (It's case sensitive).  I have found this command to come in handy for several printing issues.

-jdm

 

by: followingPosted on 2004-04-05 at 09:50:57ID: 10758901

How's it going James?  Have been able to get it work the way you desired yet?

-jdm

 

by: jamesreddyPosted on 2004-04-05 at 09:56:03ID: 10758956

Actually../I was looking over your post today.  How does your post include a partial computer name?  The way I am reading the code, it sounds as though it will be requesting the entire PC name and using that as a basis for connecting a printer.  I do not want the entire PC name, just the first part.  To use a BASIC analogy,

If %computername% begins with "NH1*" then connect to this printer...and so on.

I'm not sure I caught where the script you wrote will take that part of it out for me.  Could you break that own a little?

 

by: followingPosted on 2004-04-06 at 11:11:21ID: 10768394

This is the line that separates the room from the whole computer name:

    sRoom = Left(sComputerName, 3)

This assigns the left 3 characters of sComputerName to sRoom.  This will work fine if the first 3 characters of each room with a network printer are unique.  I choose to do it this way so that I can use the Select...Case statement that you see in my example script.  I see that you have some rooms that are 4 characters (DC25, DCRR); this will work for these two rooms because the first 3 characters are unique (sRoom will be DC2 or DCR, respectively).

However, if for example, you have a DC24 and DC25, this won't work.  You could use several If...Then...EndIf statements and the vbscript InStr function to test for the presence of strings of differing lengths.  InStr starts from the left, so should fit perfectly for your situation.  Perhaps something like the following:

If InStr(sComputerName, "NH1") > 0 Then
     sPrinterPath = "\\Server\PrinterShareName"
     WshNetwork.AddWindowsPrinterConnection sPrinterPath
     WshNetwork.SetDefaultPrinter sPrinterPath
End If

If InStr(sComputerName, "DC25") > 0 Then
     sPrinterPath = "\\Server\PrinterShareName"
     WshNetwork.AddWindowsPrinterConnection sPrinterPath
     WshNetwork.SetDefaultPrinter sPrinterPath
End If

As you can see, you may end up with quite a number of If statements doing it this way.  I definitely prefer the Select...Case way because it produces more clean, concise, easy-to-read code.

Hope this helps,
-jdm

 

by: jamesreddyPosted on 2004-04-06 at 14:37:10ID: 10769993

I'll give it a shot later this week and let you know.

 

by: jamesreddyPosted on 2004-04-23 at 10:36:24ID: 10902098

I haven't forgotten about this...been busy.

 

by: followingPosted on 2004-04-23 at 10:51:36ID: 10902249

Thanks for the update!

 

by: kabaamPosted on 2004-06-14 at 12:00:46ID: 11309456

James,
You are showing up on my clean up list of outdated questions.  Are you still working this?  :-)
Have a good day!
KaBaaM

 

by: jamesreddyPosted on 2004-07-01 at 09:39:11ID: 11448733

Ok...sorry for the incredibly long delay.  I finally got to trythis script today.  It didn't work...exactly, but your post put me on the right track.  I've been playing with this all day and came up with the following script...partially based on your post, but more so on a script I found at Microsoft's website.  This script allowed me to use more than the first three characters, if need be and offered a bit more flexibility.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Option Explicit
DIM RegEntry, ComputerName

RegEntry="HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName"
ComputerName = ReadRegValue(RegEntry)

if InStr(1,ucase(ComputerName),"NH2-",vbTextCompare) > 0 then call SetPrinter("\\little_ricki\NH2-Laser")
if InStr(1,ucase(ComputerName),"NH1-",vbTextCompare) > 0 then call SetPrinter("\\little_ricki\NH-RM1")
if InStr(1,ucase(ComputerName),"NH3-",vbTextCompare) > 0 then call SetPrinter("\\little_ricki\NH-RM3")
'so on and so forth.
wscript.quit

' ***  This subroutine installs and sets the default printer
Sub SetPrinter(ByVal PrinterPath)
  DIM WshNetwork
  Set WshNetwork = CreateObject("WScript.Network")
  WshNetwork.AddWindowsPrinterConnection(PrinterPath)
  WshNetwork.SetDefaultPrinter Printerpath
end sub

' ****  This function returns the data in the registry value
Function ReadRegValue(ByVal RegValue)      
  DIM WSHShell
  Set WSHShell = WScript.CreateObject("WScript.Shell")
  ReadRegValue=""
  On Error Resume Next
  ReadRegValue= WSHShell.RegRead(RegValue)
End Function

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Just wanted to say thanks to following for all the help and the extra time you took to help with the scripting.  Your post led me to know what to look for and thus, correct my problem with ease.

James

 

by: followingPosted on 2004-07-01 at 12:40:21ID: 11450871

Thanks James!

I like the way you were able to get around comparing a set number of characters.  I'll have to store that idea away for future scripts...

Have a nice summer,
-jdm

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...