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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Autofill Printer add script

Status
Not open for further replies.

Zappd

Technical User
May 5, 2016
22
0
0
US
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:
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
 
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
 
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?




 
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.
 
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?

 
What is in "value"? Does it match exactly any element in the array? Or does it only match a part?
 
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.


 
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)

 
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.

 
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
 
When you get a match, save the printer name.
Code:
[highlight #FCE94F]Dim SelectedPrinter[/highlight]
...
If Input <> "" Then
   found = false
   for i = 0 to ubound(PrintQueue)
      if InStr (PrintQueue(i), Input) > 0 then    
         found = true
         [highlight #FCE94F]SelectedPrinter = PrintQueue(i)[/highlight]
         exit for      
      end if
   next 
   If found Then
      wscript.echo "Printer found: " & SelectedPrinter
   Else
      wscript.echo "No match found."
   End If

end if
 
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), [highlight #FCE94F]Input[/highlight]) > 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
 
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

 
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.
 
Why do you only want the last 15 characters? Are you trying to extract the printer name without the server name?
 
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.
 
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[highlight #FCE94F], pos[/highlight]

'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
   
   [highlight #FCE94F]pos = InStrRev(SelectedPrinter, "\")[/highlight]
   [highlight #FCE94F]SelectedPrinter = Mid(SelectedPrinter, pos + 1)[/highlight]
   
   If found Then
      wscript.echo "Printer found: " & SelectedPrinter
   Else
      wscript.echo "No match found."
   End If

End If
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top