Friday, January 14, 2011

How can I install a Printer that prints to a postscript file using vbscript?

I have a requirement that through group policy all client computers needs to have a Printer installed called "PostScript Printer", every example I find online is for installing a network printer (which is not what I want).

Any help would be appreciated.

Thanks.

  • Following code did the trick for me. Not perfect, but it works.

    Option Explicit
    
    'On Error Resume Next
    Dim oWshShell, objWMIService
    
    Set oWshShell = CreateObject("WScript.Shell")
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")  
    
    Function PrinterExists(PrinterName)    
      Dim colPrinters, objPrinter
    
      Set colPrinters = objWMIService.ExecQuery _
          ("Select * From Win32_Printer Where DeviceID = '" & PrinterName & "'")  
    
      PrinterExists = False
          For Each objPrinter in colPrinters
          PrinterExists = True
      Next
    End Function
    
    Sub RenamePrinter(NameBefore, NameAfter)
      Dim colPrinters, objPrinter
      Set colPrinters = objWMIService.ExecQuery _
          ("Select * From Win32_Printer Where DeviceID = '" & NameBefore & "'")  
    
      For Each objPrinter in colPrinters
          objPrinter.RenamePrinter(NameAfter)
      Next
    End Sub
    
    Function InstallPrinter(PrinterName)
      If Not PrinterExists(PrinterName) Then
        Installing = oWshShell.Run("rundll32 printui.dll PrintUIEntry /if /f         %windir%\inf\ntprint.inf /r ""file:"" /m """ & PrinterName & """")
      End If
    
    
      Dim FailCount
      FailCount = 0
    
      Do While Not PrinterExists(PrinterName) And FailCount < 5
        WScript.Sleep 1000
        FailCount = FailCount + 1
      Loop
    
      InstallPrinter = PrinterExists(PrinterName)
    End Function
    
    Dim Installing
    
    If Not PrinterExists("PostScript Printer") Then
      'XP makes this apple printer available
      If InstallPrinter("Apple Color LW 12/660 PS") Then
        RenamePrinter "Apple Color LW 12/660 PS", "PostScript Printer"
      Else
        'Don't know of a vista one yet
      End If
    End If
    

0 comments:

Post a Comment