Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calling all VBS scripters....

Posted on 2004-03-29
14
Medium Priority
?
357 Views
Last Modified: 2010-03-18
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
Comment
Question by:jamesreddy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 5

Expert Comment

by:visioneer
ID: 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.  :)
0
 
LVL 9

Author Comment

by:jamesreddy
ID: 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.
0
 
LVL 9

Author Comment

by:jamesreddy
ID: 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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Accepted Solution

by:
following earned 1500 total points
ID: 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
0
 
LVL 3

Expert Comment

by:following
ID: 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
0
 
LVL 3

Expert Comment

by:following
ID: 10758901
How's it going James?  Have been able to get it work the way you desired yet?

-jdm
0
 
LVL 9

Author Comment

by:jamesreddy
ID: 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?
0
 
LVL 3

Expert Comment

by:following
ID: 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

0
 
LVL 9

Author Comment

by:jamesreddy
ID: 10769993
I'll give it a shot later this week and let you know.
0
 
LVL 9

Author Comment

by:jamesreddy
ID: 10902098
I haven't forgotten about this...been busy.
0
 
LVL 3

Expert Comment

by:following
ID: 10902249
Thanks for the update!
0
 
LVL 11

Expert Comment

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

Author Comment

by:jamesreddy
ID: 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
0
 
LVL 3

Expert Comment

by:following
ID: 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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Downtime reduced, data recovered by utilizing an Experts Exchange Business Account Challenge The United States Marine Corps employs more than 200,000 active-duty Marines with operations in four continents, all requiring complex networking system…
A common practice in small networks is making file sharing easy which works extremely well when intra-network security is not an issue. In essence, everyone, that is "Everyone", is given access to all of the shared files - often the entire C: drive …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question