Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript to install network printer

Status
Not open for further replies.

LindsayeCroft

Technical User
Jun 10, 2003
32
GB
Hi everyone, i work in a school and we have several computer rooms which the pupils could be in any one of. I would like to create a logon script which gets the computer name and installs the network printer in that room. so far i have the following

On error resume next
Set objNetwork = Wscript.CreateObject("WScript.Network")
Set objShell = Wscript.CreateObject("Wscript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
Set colUserEnvVars = objShell.Environment("User")
Set objNetwork = Wscript.CreateObject("Wscript.Network")

samUser = objNetwork.UserName
computerName = objNetwork.ComputerName

if objnetwork.ComputerName = "IT02" then
objNetwork.AddWindowsPrinterConnection "\\SERVER\F11"
objNetwork.SetDefaultPrinter "\\SERVER\F11"
end if


which works fine as i have only tested it with one computer name, what i would like to know is can i change it so instead of saying if computername=IT02 it says if computername starts with IT ie. so it will work for a full room of computers.

Hope this makes sense.

Thanks

Lindsay
 
Hi LindsayeCroft,
Maybe the VBScript forum was a better place for this thread.
But you can use the 'instr' function:

Description: Returns the position of the first occurrence of one string within another.
Syntax:
InStr([start, ]string1, string2[, compare])

The InStr function syntax has these arguments:
start: Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start contains Null, an error occurs. The start argument is required if compare is specified.

string1: Required. String expression being searched.

string2: Required. String expression searched for.

compare: Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. If omitted, a binary comparison is performed.

The compare argument can have the following values:
vbBinaryCompare - 0 -Perform a binary comparison.
vbTextCompare - 1 - Perform a textual comparison.


Return Values
The InStr function returns the following values:
string1 is zero-length --> 0
string1 is Null --> Null
string2 is zero-length --> start
string2 is Null --> Null
string2 is not found --> 0
string2 is found within string1 --> Position at which match is found
start > Len(string2) --> 0


Hope this helps...

--------------------------------------
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs,
and the Universe trying to produce bigger and better idiots.
So far, the Universe is winning.
 
Code:
Function TestComputerName(strPattern, strName)

Dim regex
dim objMatches

Set regex = new regexp

regex.IgnoreCase = true
regex.Pattern = strPattern

Set objMatches = regex.execute(strName)

TextComputerName = objMatches.count
end function

<snip>

if TestComputerName("IT\d\d", computerName) > 0 then
    objNetwork.AddWindowsPrinterConnection "\\SERVER\F11"
    objNetwork.SetDefaultPrinter "\\SERVER\F11"
end if

I suggest that you use regular expressions, they are a lot more flexible than the instr techniques



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top