• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

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
0
jamesreddy
Asked:
jamesreddy
1 Solution
 
visioneerCommented:
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.  :)
0
 
jamesreddyAuthor Commented:
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.
0
 
jamesreddyAuthor Commented:
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?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
followingCommented:
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
0
 
followingCommented:
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
0
 
followingCommented:
How's it going James?  Have been able to get it work the way you desired yet?

-jdm
0
 
jamesreddyAuthor Commented:
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?
0
 
followingCommented:
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

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now