blog.powershell.no

On Windows PowerShell and other admin-related topics

Adding printer drivers from a print server using Windows PowerShell

Managing printer drivers in a Remote Desktop Services (formerly Terminal Services) environment can be a challenge for administrators. There are many ways to ease this challenge, including third party solutions such as ThinPrint and UniPrint. When “thick” drivers are used on the RDS servers, all printer drivers must be installed locally on every server. Citrix XenApp has a printer driver replication feature which makes it possible to distribute printer drivers from a source server to one or more destination servers in a farm.

There might be situations where none of the mentioned solutions can be used for various reason, i.e. financial costs. A common way to solve this problem is to map all printer connections from a specified print server using administrative credentials during the initial setup of each RDS server. This solves the problem, but it can be time consuming as all printers that uses the same driver is being installed. Even though the driver is already installed on the local computer, it takes some time to process each printer connection.

I recently faced this challenge, as the installation of all printer connections from a few specified print servers took 30-40 minutes using a legacy script. I then wrote an advanced function in PowerShell to make the procedure more effective. This was accomplished by installing a printer connection only for unique printer drivers.
An example: A print server has 500 shared printer objects, while there is only 10 unique printer drivers. It would make more sense to add a printer connection (in order to install the driver) to 10 printer objects rather than 500, given the time consumed by installing a printer connection.

The function is available for download from here.

Sample usage and output:

image

 

image

There is a switch option added to the function called Clean. If this parameter is specified the function will also remove all mapped printer connections for the current user.

The function doesn’t`t provide any log options. However, it produces PowerShell objects, so it would be easy to pipe the output to a file.
An example on how to export the output objects to a csv-file:

image

But what about printer drivers added to the print servers after the function is run on an RDS server? One way to solve this could be adding the PowerShell function to a script-file which is set up as a scheduled task to run i.e. once a day on every RDS server.  If the scheduled task is set up using Group Policy Preferences, it will be automatically created on every new RDS server that is added later on. Beside scheduling the task to run at specified intervals, the print server administrator may also invoke the scheduled task remotely on every RDS server whenever a new print driver is added to a print server. The details on how to do this is previously described in this blog-post.

Since the function might be run several times on the same computer, it also checks if the driver to be installed is already in place on the local computer. This means that after the first run, only printer drivers added to the print server after the first run is actually installed. This makes the script more effective, and depending on the number of shared printer objects on the specified print server, it shouldn’t`t run for many seconds.

Coming back to the before mentioned example (~600 printer objects, ~50 unique drivers) where it took 30-40 minutes to install printer drivers from a print server, when using the Add-PrinterDriver PowerShell function the execution ran for 4 minutes. After the first run, subsequent executions ran for  20 seconds.

July 3, 2011 - Posted by | Print management, Remote Desktop Services, Scripting, Terminal Services, Windows PowerShell, Windows Server 2008, Windows Server 2008 R2 | , , , ,

7 Comments »

  1. […] wenn ich deine meinung richtig verstanden, guckmal diese link: Adding printer drivers from a print server using Windows PowerShell Wenn ich falsch verstanden, zuerst sorry, dann sag es bitte, was meinst du genau? […]

    Pingback by Druckertreiber per Powershell exportieren - MCSEboard.de MCSE Forum | September 7, 2011 | Reply

  2. Dosent work with MSCS print cluster…,

    Comment by Moeed | October 6, 2011 | Reply

  3. […] Windows PowerShell December 2, 2011 robertrieglerwien Leave a comment Go to comments http://blog.powershell.no/2011/07/03/adding-printer-drivers-from-a-print-server-using-windows-powers… Share this:PrintEmailLike this:LikeBe the first to like this post. Categories: Developement, MS: […]

    Pingback by Adding printer drivers from a print server using Windows PowerShell « MS Tech BLOG | December 2, 2011 | Reply

  4. […] Schultze on the Replicating Print Drivers in Citrix XenApp 6.5 blog post for more details.Resource :Adding printer drivers from a print server using Windows PowerShellShareHave a comment to share?One of the big reasons that I blog is because I like people and I want […]

    Pingback by Adding Printer Drivers from Print Server using PowerShell | February 2, 2012 | Reply

  5. […] Schultze on the Replicating Print Drivers in Citrix XenApp 6.5 blog post for more details.Resource :Adding printer drivers from a print server using Windows PowerShell […]

    Pingback by - Cliff Davies | February 2, 2012 | Reply

  6. I tried the link to find the Add-PrinterDriver module but it is missing. Can you please update the link to the correct location?

    Comment by rk | March 10, 2023 | Reply

    • Unfortunately, I didn`t find the scripts from my old blog where I wrote some print-related articles, but since then I think there are easier ways to install printers using native cmdlets such as Add-Printer.

      To give you a starting point:

      $ComputerName = ‘SRV01’

      $DriverName = ‘TSC ME240’
      $DriverPath = \\contoso.com\sccm\Source$\Software\PrinterDrivers\Seagull-TSCNEW
      $DriverInf = \\contoso.com\sccm\Source$\Software\PrinterDrivers\Seagull-TSCNEW\TSC.inf

      Function InstallPrinterDriver {
      Param ($DriverName, $DriverPath, $DriverInf, $ComputerName)
      $wmi = [wmiclass]\\$ComputerName\Root\cimv2:Win32_PrinterDriver
      $wmi.psbase.scope.options.enablePrivileges = $true
      $wmi.psbase.Scope.Options.Impersonation = [System.Management.ImpersonationLevel]::Impersonate
      $Driver = $wmi.CreateInstance()
      $Driver.Name = $DriverName
      #$Driver.DriverPath = $DriverPath
      $Driver.FilePath = $DriverPath
      $Driver.InfName = $DriverInf
      $wmi.AddPrinterDriver($Driver)
      $wmi.Put()
      }

      $DestFolder= \\$ComputerName\c$\Temp\Drivers
      $SrcFolder= $DriverPath

      Copy-Item $SrcFolder $DestFolder -Force -Recurse

      $DriverPath = ‘C:\Temp\Drivers\Seagull-TSCNEW\’
      $DriverInf = ‘C:\Temp\Drivers\Seagull-TSCNEW\TSC.inf’

      Invoke-Command -ComputerName xav10012 -ScriptBlock {Restart-Service spooler -Force}

      InstallPrinterDriver -DriverName $DriverName -DriverPath $DriverPath -DriverInf $DriverInf -ComputerName $ComputerName

      $PrintServer = “PS-SRV1”
      $Printers = Get-CimInstance win32_Printer -Computername $PrintServer | select Name,pscomputername

      foreach ($Printer in $Printers) {

      $ConnectionName = ‘\\’ + $PrintServer + ‘\’ + $Printer.Name

      Write-Host “Adding printer $ConnectionName” -ForegroundColor Green

      Add-Printer -ConnectionName $ConnectionName

      }

      Comment by Jan Egil Ring | March 11, 2023 | Reply


Leave a reply to Druckertreiber per Powershell exportieren - MCSEboard.de MCSE Forum Cancel reply