Merge 4 IP printer install VBScripts into one

Answered Merge 4 IP printer install VBScripts into one

  • Saturday, May 12, 2012 2:17 PM
     
      Has Code

    I have VB script that we use to allow a user to select a printer that they would like to install.   We recently purchase / upgraded some of our computers, so we now have Windows xp 32-bit & 64-bit and Windows 7 32-bit & 64-bit in our computing environment.  So we have these four vbscripts(they are the same script for all 4) the only thing different is the text file they reference, 4 text files, one for Windows xp 32 bit, one for windows xp 64-bit, one for Windows 7 32-bit and one for Windows 7 64-bit to make sure when the user installs the printer drivers, they get the correct one for there OS and if it 32-bit or 64-bit.    

    I am now in the process of trying to merge this 4 scripts into just one script and let the script determine what "kFilePath" text file it needs to reference based on the version of Windows ((XP or Windows 7)and if it is the 32-bit or 64-bit version of it.

    Attached is the Script

    Option Explicit
    
    '**********Make Changes Here**********
    const kFilePath = "\\hornet\vol1\it\Installers\Printer Drivers\Support\printers-winxp-x86.txt"	'Path to settings file <------  This is the only difference in each script based on if it is win XP 32-bit, XP 64-bit, Win 7 32-bit or Win 7 64-bit
    const kPrnPath = "\\hornet\vol1\it\Installers\Printer Drivers\Support\prnadmin.dll"	'Path to Prnadmin.dll
    const kMscPath = "\\hornet\vol1\it\Installers\Printer Drivers\Support\msvcp60.dll"	'Path to Mscvp60.dll
    '**********Make Changes Here**********
    
    const kErrorSuccess = 0
    const kErrorFailure = 1
    const kTcpRaw = 1
    const kTcpLPR = 2
    
    Dim aRecords()	'Array that stores each printer record from the text file
    Dim aFields	'Array that stores each value in a given line
       
    Main
    
    
    Sub Main
    
       Dim oFSO
       Dim oFile
       Dim aTemp		'Array that stores each line from the text file
       
    
       Dim aPrinters()	'Array that stores each printer to be installed
    
       Dim strMessage	'Menu to display to the user   
       Dim strSelection	'Stores user's input
       Dim strDefault	'Stores the name of the printer to set as the default
    
       Dim intIndex		'Generic counter
       Dim intPos		'Generic counter
       
       Dim bDefault		'Stores whether to set the printer to the Windows default
       Dim bContinue	'Stores whether user has entered correct input
       Dim bError	
       
       '***************Read Input from File*************
       Set oFSO = CreateObject("Scripting.FileSystemObject")
       Set oFile = oFSO.OpenTextFile(kFilePath,1,false) 
       aTemp = Split(oFile.ReadAll, vbCrLf)
       
       oFile.close
       Set oFile = Nothing
       Set oFSO = Nothing
    
       intPos = 0
       For intIndex = LBound(aTemp) to UBound(aTemp)
          If Len(aTemp(intIndex)) > 0 And Left(aTemp(intIndex),1) <> "'" And InStr(aTemp(intIndex),",") > 0 Then
             ReDim Preserve aRecords(intPos)
             aRecords(intPos) = aTemp(intIndex)
             intPos = intPos + 1
          End If
       Next
       aFields = Split(aRecords(0), ",")
       '***************Read Input from File*************
       
       
       
    
       'If no arguments were supplied, prompt the user to select a printer
       If WScript.Arguments.Count = 0 Then
       '***************Prompt The User*****************
          bContinue = False
      
          'Build the message to be displayed to the user
          strMessage = "Choose a printer to install (Enter the Number)" & vbCrLf & vbCrLf
          For intIndex = LBound(aRecords) to Ubound(aRecords)
             If Len(aRecords(intIndex)) > 0 And Left(aRecords(intIndex),1) <> "'" Then
         	    intPos = InStr(aRecords(intIndex),",")
     	    strMessage = strMessage & intIndex+1 & ") " & Trim(Left(aRecords(intIndex),intPos-1)) & VbCrLf
    	 End If
          Next
    
          Do Until bContinue = True
             strSelection = InputBox (strMessage, "Choose a printer")
          
             If ConvertStrToInt(strSelection) - 1 >= LBound(aRecords) And ConvertStrToInt(strSelection) - 1 <= UBound(aRecords) Then
                bContinue = True
             ElseIf Len(strSelection) = 0 Then
                Wscript.Quit   
             Else
                If Left(strMessage, 1) <> "I" Then
                   strMessage = "I'm sorry, I did not understand your input." & VbCrLf & "Please enter the corresponding number for the printer you wish to install." & VbCrLf & VbCrLf & strMessage
                End If
             End If
          Loop
    
          'Lookup the name of the selected printer and store it in the array of printers to be installed
          Redim Preserve aPrinters(0)
          intIndex = ConvertStrToInt(strSelection)-1
          aPrinters(0) = Trim(Left(aRecords(intIndex),InStr(aRecords(intIndex),",")-1))
             
          'If the printer should be the default printer, store the name of the printer in strDefault
          bDefault = MsgBox ("Would you like to make this your default printer?", VbYesNo)
          If bDefault = VbYes Then
             strDefault = aPrinters(0)
          End If
       '***************Prompt The User*****************
       Else
       '***************Retrieve Arguments**************
          'Store each printer in the array
          For intIndex = 0 to WScript.Arguments.Count - 1
             Redim Preserve aPrinters(intIndex)
    
             'If the printer should be set as the default, store it in strDefault
             If InStr(WScript.Arguments(intIndex), ",DEFAULT") > 0 Then
                strDefault = Left(WScript.Arguments(intindex), InStr(WScript.Arguments(intIndex),",DEFAULT")-1)
                aPrinters(intIndex) = strDefault
             Else
                aPrinters(intIndex) = WScript.Arguments(intIndex)         
             End If
          Next
       End If
       '***************Retrieve Arguments**************
    
    
       '***************Register DLLs**************
       'Install the necessary DLL files
       RegisterDLLs
       '***************Register DLLs**************
      
      
      
       '***************Install Each Printer**************
       'Loop through each printer to be installed
       For intIndex = 0 to UBound(aPrinters)
           bError = SetupPrinter(aPrinters(intIndex), strDefault)
       Next
    
       If bError = True Then
          MsgBox ("An error occured during printer installation." & VbCrLf & "Please consult your Network Administrator to install the printer manually.")
       Else
          MsgBox ("Printer installation completed successfully.")
       End If
       '***************Install Each Printer**************
    End Sub
    
    
    
    
    
    
    
    Function SetupPrinter(strPrinter, strDefaultPrinter)
       Dim strComputer		'Name of the computer to install the printer on
       Dim strPortName		'Name of the port to use
       Dim strPortType		'The type of port to create
       Dim strHostAddress		'The port's IP address
       Dim strPortNumber		'The port number to use when creating the port
       Dim strPrinterName		'The printer's name in Windows
       Dim strPrinterDriver		'The driver's name
       Dim strPrinterDriverPath	'The path to the driver's INF file
            
       Dim intIndex
       Dim bError   	'Stores whether or not an error has occurred
          
       'Search for the printer name on each line of the printer setup file
       For intIndex = 0 to UBound(aRecords)
          'If the printer name to install matches the line in the printer setup file,
          'retrieve the information from that line
          If InStr(aRecords(intIndex), strPrinter) > 0 Then
             aFields = Split(aRecords(intIndex), ",")      
                  
             strComputer = ""	'Default setting for local computer
             strPrinterName = Trim(aFields(0))
             strHostAddress = Trim(aFields(1))
             strPortNumber = Trim(aFields(2))
             strPortType = "RAW"
             strPrinterDriver = Trim(aFields(3))
             strPrinterDriverPath = Trim(aFields(4))
                  
             Exit For
          End If
       Next
       
       'If information for the selected printer was found, attempt to install the printer
       If Len(strPrinterName) > 0 Then   
          'Create the TCP port
          strPortName = CreatePort(strComputer, strHostAddress, strPortNumber, strPortType)
           
          'Install the printer driver
          InstallPrinterDriver strPrinterDriver, strPrinterDriverPath
          
          'Create the printer
          If InstallPrinter(strComputer, strPrinterName, strPrinterDriver, strPortName) = True Then
             If strDefaultPrinter = strPrinterName Then SetDefaultPrinter strPrinterName
             bError = False
          Else
             bError = True
          End If
       End If
    
       SetupPrinter = bError
       
    End Function
       
       
    '**********Creates IP Port**********
    Function CreatePort(strComputer, strHostAddress, strPortNumber, strPortType)
    
       Dim oPort
       Dim oMaster
       Dim bPortExists
       Dim strPortName
    
       On Error Resume Next
    
       strPortName = "IP_" & strHostAddress & ":" & strPortNumber
    
       set oPort = CreateObject("Port.Port.1")
       set oMaster = CreateObject("PrintMaster.PrintMaster.1")
    
       oMaster.PortGet strComputer, strPortName, oPort
       If Err.Number = kErrorSuccess Then
          'Port already exists
          bPortExists = True
       Else
          'Port does not already exist, clear the error
          Err.Clear
       End if
    
       oPort.ServerName = strComputer
       oPort.PortName = "IP_" & strHostAddress & ":" & strPortNumber
       oPort.HostAddress = strHostAddress
       oPort.PortNumber = strPortNumber
       oPort.SNMP = false
    
       Select Case Ucase(strPortType)
          Case "RAW"
    	   oPort.PortType = kTcpRaw
          Case "LPR"
       	   oPort.PortType = kTcpLPR
       End Select
    
       If bPortExists = False then
    	oMaster.PortAdd oPort
       Else
    	oMaster.PortSet oPort
       End if
    
       If Err.Number <> kErrorSuccess Then 
    	MsgBox ("Error creating port:" & VbCrLf & err.number & ": " & Err.Description) 
       End if
    
       CreatePort = strPortName
       
    End Function
    '**********Creates IP Port**********
    
    
    '**********Installs Printer Driver**********
    Function InstallPrinterDriver(strPrinterDriver, strPrinterDriverPath)
    
       Dim oShell
       
       Set oShell = Wscript.CreateObject("Wscript.Shell")
       
       If Len(strPrinterDriverPath) <> 0 And Len(strPrinterDriver) <> 0 Then
    	oShell.Run "cmd /c rundll32 printui.dll,PrintUIEntry /ia /f """ & strPrinterDriverPath & """ /m """ & strPrinterDriver & """ /h ""Intel"" /v ""Windows 2000""", 0, True
       End If
    
    End Function
    '**********Installs Printer Driver**********
    
    
    '**********Creates the Printer**********
    Function InstallPrinter(strComputer, strPrinterName, strDriverName, strPortName)
    
       Dim oMaster
       Dim oPrinter
       Dim bPrinterExists
    
       On Error Resume Next
       
       Set oMaster  = CreateObject("PrintMaster.PrintMaster.1")
       Set oPrinter = CreateObject("Printer.Printer.1")
    
       oMaster.PrinterGet strComputer, strPrinterName, oPrinter
    
       If Err.Number = kErrorSuccess Then
          'Printer already exists
          bPrinterExists = true
       Else
          'Printer does not already exist, clear the error
          Err.Clear
       End If
       
       oPrinter.ServerName = strComputer
       oPrinter.PrinterName = strPrinterName
       oPrinter.DriverName = strDriverName
       oPrinter.PortName = strPortName
       
       If bPrinterExists = False Then
          oMaster.PrinterAdd oPrinter
       Else
          oMaster.PrinterSet oPrinter
       End if
       
       If Err.Number <> kErrorSuccess Then 
          MsgBox ("Error creating the printer:" & VbCrlF & Err.Number & ": " & Err.Description)
          InstallPrinter = False
       Else
          InstallPrinter = True
       End if
    
    End Function
    '**********Creates the Printer**********
    
    
    '**********Registers Required DLLs*********
    Sub RegisterDLLs()
       
       Dim oShell
       
       Set oShell = Wscript.CreateObject("Wscript.Shell")
    
       oShell.Run "cmd /c regsvr32 /s """ & kMscPath & """", 0, True
       oShell.Run "cmd /c regsvr32 /s """ & kPrnPath & """", 0, True
          
    End Sub
    '**********Registers Required DLLs*********
    
    
    '**********Sets the Default Printer*********
    Sub SetDefaultPrinter(strPrinter)
    
       Dim oShell
       
       Set oShell = Wscript.CreateObject("Wscript.Shell")
       
       If Len(strPrinter) <> 0 Then
    	oShell.Run "cmd /c rundll32 printui.dll,PrintUIEntry /y /n """ & strPrinter & """", 0, True
       End If
       
    End Sub
    '**********Sets the Default Printer*********
    
    
    '****************Converts a string to an integer****************
    Function ConvertStrToInt(strNumber)
    	
    	Dim intIndex
    	Dim intResult
    	Dim intDigit
    
    	For intIndex = 1 to Len(strNumber)
    		Select Case Mid(strNumber, intIndex, 1)
    			case "0"
    				intDigit = 0
    			case "1"
    				intDigit = 1
    			case "2"
    				intDigit = 2
    			case "3"
    				intDigit = 3
    			case "4"
    				intDigit = 4
    			case "5"
    				intDigit = 5
    			case "6"
    				intDigit = 6
    			case "7"
    				intDigit = 7
    			case "8"
    				intDigit = 8
    			case "9"
    				intDigit = 9
    		End Select
    		
    		intResult = intResult * 10 + intDigit
    	Next
    
    	ConvertStrToInt = intResult
    
    End Function
    '****************Converts a string to an integer****************

    Any guidance would be very helpful

    JOEY

     

All Replies

  • Saturday, May 12, 2012 3:45 PM
     
     Answered

    You need to ask a question.

    Your script is very long.  I doubt that anyone wants to go through it and try and guess what your question is.

    Start small. What is the first problem you are having?

    In WIndows AD 2000 and later we generally allow teh print spooler to install teh printer drivers as needed.  This is automatic and will also maintain all updates. Users need only choose which spoller service they wich to prot to.  Everythig else is automatic.

    Welcome to the 21st century Windows Platform.   We have not had to do manual driver installs since Windows NT4.

    You should also note that the script was likely written by someone who did not know scripting at all.  Ther is one (actually more) fiunction that shows this: ConvertStrToInt.

    This function can be replaced by CInt(string) which is built into all VB languges.

    http://www.w3schools.com/vbscript/func_cint.asp

    Do yourself a favor and set printing up in a post-W2K way.  Group Policy can deploy pronters per-department, per-OU, per PC, per-users.  Your choice.

    G. L.


    ¯\_(ツ)_/¯


  • Thursday, May 17, 2012 6:57 PM
    Moderator
     
     Answered

    Hi,

    We're glad to help, but we don't have the resources to rewrite long scripts for people or to redesign scripts per requested specifications. Also, you should explore other options as suggested by jrv.

    Bill