Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: powershell printer question  (Read 10682 times)

0 Members and 1 Guest are viewing this topic.

wbrost

    Topic Starter


    Intermediate
  • Thanked: 11
    powershell printer question
    « on: February 22, 2011, 06:30:09 AM »
    Can some one please point me to an example of a Powershell script that installs a TCP/IP printer? unfortunately our department decided to stop using print servers so now we are forced to add printers using print to IP. I am looking for a script that will install a printer and use a TCP/IP port.

    Thanks,
    Wbrost

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: powershell printer question
    « Reply #1 on: February 23, 2011, 06:59:55 AM »
    This may help. It seems straightforward. Create instances of two WMI classes and fill in the properties with your local information.

    Good luck.  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    wbrost

      Topic Starter


      Intermediate
    • Thanked: 11
      Re: powershell printer question
      « Reply #2 on: February 28, 2011, 08:57:27 AM »
      ok getting closer. I can get the first portion of the code to work but during the second part I get the following error message:

      Exception calling "Put" with "0" argument(s): "Generic failure "
      At line:1 char:19
      + $objNewPrinter.Put <<<< ()
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : DotNetMethodException

      Below is the code that I am using to get this working.

      $objPrint = [WMICLASS]"Win32_Printer"
      $objPrint.psbase.scope.options.EnablePrivileges = $true
      $objNewPrinter = $objPrint.createInstance()
      $objNewPrinter.DeviceID = "Rhyne_235A_02"
      $objNewPrinter.DriverName = "TOSHIBA e-STUDIO3510cSeriesPLC6"
      $objNewPrinter.PortName = "Toshiba_3510C"
      $objNewPrinter.Shared = $false
      $objNewPrinter.Put()

      thanks,
      wbrost

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: powershell printer question
      « Reply #3 on: March 01, 2011, 04:34:39 PM »
      I ran into the same problem and after some tinkering around at the Powershell command line, I suspect the problem is not with the put instruction but with the value of the properties. Specifically with the Drivername property.

      It seems you can create a printer without a physical unit, but the driver needs to be installed. I was able to get this snippet to work simply by using the installed driver of a real printer on my system. Be sure the same port name used in creating the port is used when creating the printer.

      Code: [Select]
      # -----------------------------------------------------------------------------
      # Script: PSH-WMI-setTCPIPPrinterPortInstall.ps1
      # Author: Powershell Community
      # Date: 02/22/2011 11:07:40
      # Comments: Install a printer to TCP/IP port
      # -----------------------------------------------------------------------------
      #Requires -Version 2.0

      $PortName = "MyPort"
      $PrintDriver = "HP Deskjet 930C/932C/935C"

      #Create the port
      #
      $objPort = [wmiclass] "Win32_TcpIpPrinterPort"
      $objNewPort = $objPort.CreateInstance()

      #$objNewPort.psbase.scope.options.EnablePrivileges = $False    #if using W2K3 use $True
      $objNewPort.Name = $PortName
      $objNewPort.HostAddress = "10.10.10.10"
      $objNewPort.Protocol = 1                                      #1=RAW (default) 2=LPR
      $objNewPort.Put()

      #Create the Printer
      #
      $printer = [wmiclass] "Win32_Printer"
      $newprinter = $printer.CreateInstance()

      $newprinter.DeviceID = "My New Printer"    #This property req'd
      $newprinter.Drivername = $PrintDriver      #This property req'd
      $newprinter.PortName = $PortName           #This property req'd

      $newprinter.Shared = $true
      $newprinter.Sharename = "ShareName"
      $newprinter.Location = "Office"
      $newprinter.Comment = "Comment"
      $newprinter.Put()

      <#
      Use Get-Member on $newprint and $objNewPort to retrieve all properties
      and methods available
      #>

      Good luck.  8)

      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      wbrost

        Topic Starter


        Intermediate
      • Thanked: 11
        Re: powershell printer question
        « Reply #4 on: March 04, 2011, 01:17:50 PM »
        I ran into the same problem and after some tinkering around at the Powershell command line, I suspect the problem is not with the put instruction but with the value of the properties. Specifically with the Drivername property.

        It seems you can create a printer without a physical unit, but the driver needs to be installed. I was able to get this snippet to work simply by using the installed driver of a real printer on my system. Be sure the same port name used in creating the port is used when creating the printer.


        If any one is having this issue you can try the neat little utility MS provides to do this.  And the best part is you can point to an INF over the network. This is how I solved the issue I was having above.

        C:\Users\ss947bw>pnputil /?
        Microsoft PnP Utility
        Usage:
        ------
        pnputil.exe [-f | -i] [ -? | -a | -d | -e ] <INF name>
        Examples:
        pnputil.exe -a a:\usbcam\USBCAM.INF      -> Add package specified by USBCAM.INF
        pnputil.exe -a c:\drivers\*.inf          -> Add all packages in c:\drivers\
        pnputil.exe -i -a a:\usbcam\USBCAM.INF   -> Add and install driver package
        pnputil.exe -e                           -> Enumerate all 3rd party packages
        pnputil.exe -d oem0.inf                  -> Delete package oem0.inf
        pnputil.exe -f -d oem0.inf               -> Force delete package oem0.inf
        pnputil.exe -?                           -> This usage screen