Link to home
Start Free TrialLog in
Avatar of jamesreddy
jamesreddy

asked on

Calling all VBS scripters....

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
Avatar of visioneer
visioneer

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:
https://www.experts-exchange.com/questions/20936760/How-to-set-printers-and-default-printers-on-WinXP-clients-logging-into-Win2003-domain.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.  :)
Avatar of jamesreddy

ASKER

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.
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?
ASKER CERTIFIED SOLUTION
Avatar of following
following
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
How's it going James?  Have been able to get it work the way you desired yet?

-jdm
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?
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

I'll give it a shot later this week and let you know.
I haven't forgotten about this...been busy.
Thanks for the update!
James,
You are showing up on my clean up list of outdated questions.  Are you still working this?  :-)
Have a good day!
KaBaaM
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
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