Jump to content

Recommended Posts

Posted

Greetings! I, too, am attempting to use a .vbs script to assign specific printers to specific computers in specific rooms. I've been reading this, and other threads, on the subject matter, seeing some of the same scripting concept that I'm attempt to use.

Here is my dilemma-I have named each of my clients with the following structure: RM400-1-PRN1 with the "PRN1" to be used to indicate, in the scripting, the specific printer. In my script, I use the " = right(xxxxx, 3)" but it does not seem to be reading the "PRNx" and mapping the specified printer. Below is the script that I'm trying to use:

 

Example 1:

 

Dim wshNetwork

Dim strUsername

Dim intCnt

Dim CurrentUser

Dim strCompName

Dim strShortName

Dim PrinterPath

Set wshNetwork = CreateObject("WScript.Network")

 

On Error Resume Next

 

wshNetwork.RemoveNetworkDrive "h:"

wshNetwork.RemoveNetworkDrive "P:"

 

wshNetwork.MapNetworkDrive "h:", "\\xxxx\" & wshNetwork.UserName & "$"

 

strCompName = wshNetwork.ComputerName

 

strShortName = RIGHT(strCompName,4)

Select Case strShortName

 

Case "PRN1"

PrinterPath = "\\xxxx\LAB1(KOELLER)"

wshNetwork.AddWindowsPrinterConnection PrinterPath

wshNetwork.SetDefaultPrinter "\\xxxx\LAB1(KOELLER)"

 

Case "PRN3"

Set objWSHNetwork = CreateObject("WScript.Network") 'create network object

strConnectString = "\\xxxx\lab2 (open)"

strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

wshNetwork.SetDefaultPrinter "\\xxxx\lab2 (open)"

 

End Select

 

WScript.Quit

 

or

 

Example 2:

 

Dim wshNetwork

Dim strUsername

Dim intCnt

Dim CurrentUser

Dim strCompName

Dim strShortName

Dim PrinterPath

Set wshNetwork = CreateObject("WScript.Network")

 

On Error Resume Next

 

wshNetwork.RemoveNetworkDrive "h:"

wshNetwork.RemoveNetworkDrive "P:"

 

wshNetwork.MapNetworkDrive "h:", "\\xxxx\" & wshNetwork.UserName & "$"

 

 

strCompName = wshNetwork.ComputerName

strShortName = RIGHT(strCompName,4)

Select Case strShortName

 

Case "PRN3"

PrinterPath = "\\xxxx\MediaCtr3500"

wshNetwork.AddWindowsPrinterConnection PrinterPath

wshNetwork.SetDefaultPrinter "\\xxxx\MediaCtr3500"

 

End Select

 

WScript.Quit

 

("xxxx" is sever name)

 

If anyone has any suggestions and/or knows what needs to be fixed to correct the problem, it would be greatly appreciated!

 

Behuck

_________________

Posted
What is it reading in? (if anything at all) do an echo to see what is being read. You may want to covert the the string to lowercase or uppercase as well before doing your case statements.
Posted

what happens when you run this script on a stand alone machine , also if you get a msgbox to show you what the strShortName stores ie

 

msgbox strShortName

 

so that when you run the script you can see what strShortName contains exactly.

 

when ChrisH says what it is reading in, he means what does the strShortName contain by using a messagebox to prompt you with what that variable contains

Posted

strShortName = RIGHT(strCompName,4)

 

After this do a

 

Wscript.echo strShortName

 

I would also try changing the case in the same spot as well.

Posted

Chris & Gecko,

 

Thx for your input so far - appeciated. I'm not very good with scripting, although I want to learn (and I'm doing so by reading all the posts in this thread-very helpful). I got this script from another "tech" friend at a tech conference and I'm trying to get it to work for me.

 

Chris - I'll place the code line "Wscript.echo strShortName" in the script and run it when I get to school on Monday. Please, excuse my ignorance again, but what are you referring to about the "case" and where at exactly(do you mean from "RIGHT" to "right" or are you referring to "PRN1" to "prn1")? I understand it has to do with either upper or lower case, but I'm not sure where you are talking about exactly.

 

The other problem I'm having with this script at this time is that when, logged on as a "test student" and connecting to my terminal server (MS Office is located/accessed), I'm in Word and attempt to print a test page, it states that no printers are installed - the script is not following the student's log onto the T-server; I've set the "remote connection" to connect to "printers", but the scripting doesn't seem to follow.

 

Thx again for all your advise/assistance!

Posted

how are you assigning this script ? as a logon or startup script ?

 

T-server ? Is that a terminal server ?

 

Im guessing you have all your users that need this script in a specific OU in Active Directory ? Could be wrong * shrugs *

Posted

the other suggestion I have is instead of using right(strShortName,4) and assigning that to a variablbe would be to use the split function instead.

 

The reason I say this is because what happens if the PRN numeric value is greater then 1 in length ie PRN30 or PRN02 ??

 

strCompName = wshNetwork.ComputerName
strShortName = RIGHT(strCompName,4)

myarray = Split(strShortName,"-")
msgbox myarray(0)
msgbox myarray(1) 
msgbox myarray(2)   '<--- This will be the code that will 
                               ' return the relevant
         ' machine name ( minus the msgbox )

 

Then you would have your select case statements ie

 

Select Case myarray(2)

Case "PRN1"  '<-- The items in between the quotes are obviously the name of the printer as well as the suffix after the machine name etc so if the machine name matches the suffix ie

RM400-1-PRN1, then any code you have after the case above ie 
Case "PRN1" then the code that is directly underneath this case will be run each time any machine that the script finds that has PRN1 at the end of it.

So you would now put your code here to do what you need it to do , ie map a printer(s) and set a default one and or map network drives for any machines that end in PRN1

Case "PRN2"

Map any printers or network drives / shares if PRN2 is found

Case Else

' you can use case else to prompt the user or yourself if any errors occur
' you dont need this in here , if this causes problems then delete the
' case else along with msgbox err.description.

MsgBox err.description

' Now that you have all the different cases ie Case "PRN1" , Case "PRN2"
' etc you need to tell it that you have done all of them by doing the 
' following

End Select

' A select case statement is like an if.... else statement but it is easier to

' read and follow and understand.

 

As for making it follow the user, Im not so sure about that until we establish that the script itself is actually running and that the chunks of code are working with regards to the select case strShortName and making sure that the sub chunks of code underneath PRN1 etc are running as well.

Posted

If you are usign terminal server then the client computer names will not be picked up as desired using the code you have pasted.

You need to expand an environment string to find out the client ID. If you look at Rics script in the scripts section you will see this

 

if (computerName = "svrts1") then _
computerName = LCase(WshShell.ExpandEnvironmentStrings("%CLIENTNAME%"))

 

What this is doing is saying if the computer name is the terminal server then get the correct client name by expanding the string %clientname% which will return the true name of the client and not of the terminal server.

 

As to you question of case I am talking about upper and lower case.

Before you compare the name of the computer name it is best to change it to all one case because some of you clients maybe "RM400-1-PRN1" and some might be "rm400-1-prn1" or similar and will make the case statement fail top find anything. It may not be the case but it is good practice to do this kind of thing to stop any weird result because someone didnt press shif/have caps lock on when they were naming the PC.

Posted

If that is true about the upper and lower case then when using split if you do

 

select case LCase(myarray(2))

 

Case "prn1"

 

Case "prn2"

 

End Select

 

Also if you assign strShortName the expanded client machine name as per chri's suggestion instead of doing it the other way if it is using a terminal server.

 

That way when you split the machine name it gets the true machine name.

Posted

Chris & Gecko,

 

The "T-server" is mentioned is my shortcut for Terminal Server.

 

The script is a logon script, set for when each student logons to a client.

 

Yes, I have all students in a AD OU - "Students" then listed in individual graduation OUs (2007, 2008, etc.)

 

All my clients are named using uppercase (example)"RM400-1-PRN1". The "PRNx" goes only from PRN1 to PRN8, as I have a total of 8 networked printers available for staff/students.

 

I will attempt to try both your suggestions when I get back to work on Tuesday (Spring Holiday now).

 

THX so much for all your help - God Bless

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...