×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Autofill Printer add script

Autofill Printer add script

Autofill Printer add script

(OP)
Hello,
This is my first time posting so please let me know if more information is required.
This is a script to add printers that is ran from the local desktop. The issue is that the naming scheme of the printers changed.
The original script only required four digits (ID) and the script would run because all the names start with the same information. If the printer was not found on the server it would exit the script, otherwise it install the printer and ask if the printer is default.
The current script requires type, model and ID before it has enough information to run.

Original name Ex. PRINTPRNAME1234 (this requires the ID)
PRINT= item, PR= Printer type, NAME= name of printer, 1234=Printer ID

Now the name has more options.
Type="MC=Multi-Function, PL=Plotter, PR=Printer", Model="DE, HP, KM, LX"
New name Ex. PRINTMCHP123456 (this requires type, model and ID)
PRINT= item, MC= type, HP= model, 123456= ID

The below script works, but it requires the person to manually add the information to create the name.
Is there any way it can automatically search the server using the ID and if it finds the ID automatically fill in the type and model?
Thanks.

CODE --> VBScript

On Error Resume Next
Dim MSG, MSG1, MSG2, MSG3, MSG4
Dim Style, Style2, Style4
Dim Title, Title1, Title2, Title4
Dim Response, Response1, Response2, Response3, Response4
Dim AddPrinter, SetDefault, PrinterName

MSG = "Install a Network Printer?"
Style = vbOKCancel + vbQuestion
Title = "Network Printer Install"
MSG1 = "MC=Multi-Function, PL=Plotter, PR=Printer"
MSG2 = "Enter Model, DE, HP, KM, LX"
MSG3 = "Enter 6 digit ECN"
Style4 = "Default Printer?"
Title4 = "Y"

AddPrinter = False
SetDefault = False
  
Response = MsgBox(MSG, Style, Title)
Set WshNetwork = CreateObject("WScript.Network")

If Response = 1 Then
 
   Response1 = InputBox(MSG1, Style)
   Response1 = UCase(Response1)
   	if Response1 <> "" then
   Response2 = InputBox(MSG2, Style)
   Response2 = UCase(Response2)
	if Response2 <> "" then
   Response3 = InputBox(MSG3, Style)   	
	if Response3 <> "" then	
		
	PrinterName = "\\PRINTSERVER01\PRINT" & Response1 & Response2 & Response3
	AddPrinter = True

        MSG4 = "Make printer: " & PrinterName & " the Default Printer (OK=Yes/Cancel=No)"
	Response4 = InputBox(MSG4, Style4, Title4)
		If Response4 = "Y" Then
			SetDefault = True
		Else
			SetDefault = False
		End If

        If AddPrinter = True Then
           WshNetwork.AddWindowsPrinterConnection PrinterName
           If SetDefault = True Then
             WshNetwork.SetDefaultPrinter PrinterName
           End If
	End if
	End if
	End If
Else
  AddPrinter = False
  MsgBox "Operation Aborted!"
End If

Else
   AddPrinter = False
   SetDefault = False
   MsgBox "Operation Aborted!"
End If 

RE: Autofill Printer add script

You can get a collection with:
Set PrinterConnections = WSHNetwork.EnumPrinterConnections
First arguments of InputBox are: Prompt, Title, Default. Do you see what you expect for Responses 1 - 3?

combo

RE: Autofill Printer add script

(OP)
combo,

I'm not really sure how to use this.
Did some research but was unable to find a way to add where it would work.
Is it supposed to added to the current script or replace something in the script?




RE: Autofill Printer add script

EnumPrinterConnections does not work on remote computers as far as I know.

Try the script below (source: social.technet.microsoft.com)

CODE

Dim ServerName, Computer, PrintQueue

ServerName = "PRINTSERVER01"

Set Computer = GetObject("WinNT://" & ServerName & ",Computer")
Computer.Filter = Array("PrintQueue")
For Each PrintQueue In Computer
  WScript.Echo PrintQueue.PrinterName
Next 

I have not tried it, but it hopefully will list the printers available on the print server to give you a start.

RE: Autofill Printer add script

(OP)
guitarzan,

That lets me put the \\servername\printername into an array.
How do I search the array and pull the printername?

I have tried using code like the following but am not able to get it to work.

CODE -->

found = false
for i = 0 to ubound(iArray)
    if iArray(i) = value then    
        found = true
        exit for       
    end if
next 

Since the data is now in an array I would to be able to search the array for a specific ID.
If found pull the data (printername) and continue the code from there.
Is that something you can help with?

RE: Autofill Printer add script

What is in "value"? Does it match exactly any element in the array? Or does it only match a part?

RE: Autofill Printer add script

(OP)
Here is what I was trying to test:

CODE -->

Dim ServerName, Computer, PrintQueue

ServerName = "Printserver"

Set Computer = GetObject("WinNT://" & ServerName & ",Computer")
Computer.Filter = Array("PrintQueue")
For Each PrintQueue In Computer
 rem WScript.Echo PrintQueue.PrinterName
Next 

found = false
for i = 0 to ubound(PrintQueue)
    if PrintQueue(i) = "001598" then    
	msgbox("found")
        found = true
        exit for      
    end if
next 

001598 is the last part of the printer name.
What I'm trying to do is to incorporate this into the above script.
What I want to do is have a message box that comes up and asks for the ID (which it does). Once the ID is entered it will build the array(thanks for that) and then search for a printer with the ID that was provided.
From there the script will build the printer.


RE: Autofill Printer add script

OK, assuming "value" is the ID, a few options would be:

CODE

if Right(PrintQueue(i), Len(value)) = value then 
True if the last characters of PrintQueue(i) match "value"

CODE

if InStr(PrintQueue(i), value) > 0 then 
True if "value" is found anywhere inside PrintQueue(i)

RE: Autofill Printer add script

(OP)
Thanks for the input. I have been searching for a few days trying to figure this out.
Here is what I finally came up with:

CODE -->

On Error Resume Next

Dim ServerName, Computer, PrintQueue

ServerName = "PRINTSERVER"

Set Computer = GetObject("WinNT://" & ServerName & ",Computer")
Computer.Filter = Array("PrintQueue")
For Each PrintQueue In Computer
  'WScript.Echo PrintQueue.PrinterName 
Next 

Input = InputBox ("Enter ECN", "Find Printer")
If Input <> "" Then

 found = false
 for i = 0 to ubound(PrintQueue)
     if InStr (PrintQueue(i), Input) > 0 then    
         found = true	 
         exit for      
     end if
 next 
end if 
What I can't figure out is how to search an array. Really have no idea if the search function is even correct.
I want to have an input box come up and enter a number.
Then search the array for that number.
If found capture the entire name.
From there I should be able to continue.

Example:
Search for 1598
if found
pull entire name which is PRINTMCHP001598

I really appreciate your assistance with this.

RE: Autofill Printer add script

Just pick the text or index inside the loop when found:
found = false
foundtext=""
for i = 0 to ubound(PrintQueue)
if InStr (PrintQueue(i), Input) > 0 then
foundtext=PrintQueue(i)
found = true
exit for
end if
next
end if

combo

RE: Autofill Printer add script

When you get a match, save the printer name.

CODE

Dim SelectedPrinter
...
If Input <> "" Then
   found = false
   for i = 0 to ubound(PrintQueue)
      if InStr (PrintQueue(i), Input) > 0 then    
         found = true
         SelectedPrinter = PrintQueue(i)
         exit for      
      end if
   next 
   If found Then
      wscript.echo "Printer found: " & SelectedPrinter
   Else
      wscript.echo "No match found."
   End If

end if 

RE: Autofill Printer add script

(OP)
I have tried both codes and still not working.
Like I said this is my first post so sorry if I'm not providing you with the correct information.
combo,
Do I need to Dim foundtext?
I'm guessing that should be the output when it works?

guitarzan,
when I run this all that comes up is "Printer found:"
If I do a "wscript.echo Input" on either it shows what I entered in the InputBox.
But, if I wscript.echo either foundtext or SelectedPrinter it shows nothing.
It's like the array cannot recognize the Input from the InputBox.
From my understanding

CODE -->

if InStr (PrintQueue(i), Input) > 0 then 
is where all of the magic (search array for Input) should happen.
Do I need to convert Input into something that the array can recognize?
Honestly I have no idea if that is a valid question and only ask because of what I have been researching.
I receive no errors from either script.
Zappd

RE: Autofill Printer add script

OK, tested so now I see the issue. First of all, the reason you see no errors is that you have "On Error Resume Next", which suppresses all runtime errors. Get rid of that.

The next issue is that, you should just go through the object's elements using the method in my code above.

Finally, it's good practice to put "Option Explicit" at the top of your scripts, which forces you to "Dim" all variables. This prevents you from having a typo in a variable name, which can give unexpected results.

Something like this should work:

CODE

Option Explicit
Dim ServerName, Computer, PrintQueue, Input, found, SelectedPrinter

ServerName = "PRINTSERVER"

Set Computer = GetObject("WinNT://" & ServerName & ",Computer")
Computer.Filter = Array("PrintQueue")
'For Each PrintQueue In Computer
'  WScript.Echo PrintQueue.PrinterName
'Next

Input = InputBox ("Enter ECN", "Find Printer")
If Input <> "" Then

   found = False
   For Each PrintQueue In Computer
      If InStr(PrintQueue.PrinterName, Input) > 0 then    
         found = True
         SelectedPrinter = PrintQueue.PrinterName
         Exit For      
      End If
   Next 
   
   If found Then
      wscript.echo "Printer found: " & SelectedPrinter
   Else
      wscript.echo "No match found."
   End If

End If 

RE: Autofill Printer add script

(OP)
Great work.
I'm trying to learn...
Do you see any issue with me using this to get the printer name?

CODE -->

SelectedPrinter = Right(PrintQueue.PrinterName, 15) 
Once again thanks so much for all of your help with this.

RE: Autofill Printer add script

Why do you only want the last 15 characters? Are you trying to extract the printer name without the server name?

RE: Autofill Printer add script

(OP)
Yes, and wanted to know if you saw any issues with pulling the name alone like that.
Was planning on using SelectedPrinter as the variable to build the printer.

RE: Autofill Printer add script

That only works if the printers will always be exactly 15 characters. The below would not be dependent on name length... it captures all the characters after the last backslash. See Mid() and InStrRev() for how those functions work.

CODE

Option Explicit
Dim ServerName, Computer, PrintQueue, Input, found, SelectedPrinter, pos

'ServerName = "PRINTSERVER"
ServerName = "192.168.18.2"

Set Computer = GetObject("WinNT://" & ServerName & ",Computer")
Computer.Filter = Array("PrintQueue")
'For Each PrintQueue In Computer
'  WScript.Echo PrintQueue.PrinterName
'Next

Input = InputBox ("Enter ECN", "Find Printer")
If Input <> "" Then

   found = False
   For Each PrintQueue In Computer
      If InStr(PrintQueue.PrinterName, Input) > 0 then    
         found = True
         SelectedPrinter = PrintQueue.PrinterName
         Exit For      
      End If
   Next
   
   pos = InStrRev(SelectedPrinter, "\")
   SelectedPrinter = Mid(SelectedPrinter, pos + 1)
   
   If found Then
      wscript.echo "Printer found: " & SelectedPrinter
   Else
      wscript.echo "No match found."
   End If

End If 

RE: Autofill Printer add script

(OP)
Here is the final product(for now).

CODE -->

Option Explicit
Dim ServerName, Computer, PrintQueue, Input, found, SelectedPrinter
Dim AddPrinter, SetDefault, PrinterName, MSG, Style, Title
Dim Response, WshNetwork

Title = "Y"

ServerName = "PRINTSERVER"

AddPrinter = False
SetDefault = False


'Create Array
Set Computer = GetObject("WinNT://" & ServerName & ",Computer")
Computer.Filter = Array("PrintQueue")

Input = InputBox ("Enter ECN to install printer or Cancel to exit", "Printer Install")
If Input <> "" Then
   found = False
   For Each PrintQueue In Computer
      If InStr(PrintQueue.PrinterName, Input) > 0 then    
         found = True
         SelectedPrinter = PrintQueue.PrinterName
         Exit For      
      End If
   Next
 
'Build printer
   If found Then
   Set WshNetwork = CreateObject("WScript.Network") 	
	AddPrinter = True
	PrinterName = SelectedPrinter
	   if PrinterName = "" then response.write("No")
   
	AddPrinter = True
	MSG = "Make printer: " & PrinterName & " the Default Printer (OK=Yes/Cancel=No)"'
	Response = InputBox(MSG, Style, Title)
	  If Response = "Y" Then
		SetDefault = True
	  Else
		SetDefault = False
	  End If
	If AddPrinter = True Then
           WshNetwork.AddWindowsPrinterConnection PrinterName
              If SetDefault = True Then
           WshNetwork.SetDefaultPrinter PrinterName
        End If
   End If
   Else
      MsgBox "Printer not found. Verify ECN and try again."
   End If

Else
   AddPrinter = False
   SetDefault = False
   MsgBox "Install Cancelled"
End If 
Appreciate all of your help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close