Reader small image

You're reading from  Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

Product typeBook
Published inSep 2017
Reading LevelBeginner
Publisher
ISBN-139781787122048
Edition2nd Edition
Languages
Right arrow
Authors (2):
Thomas Lee
Thomas Lee
author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

 Ed Goad
Ed Goad
author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad

View More author details
Right arrow

Chapter 4. Managing Printers

In this chapter, we cover the following recipes:

  • Installing and sharing printers
  • Publishing a printer
  • Changing the spool directory
  • Changing printer drivers
  • Printing a test page on a printer
  • Reporting on printer security
  • Modifying printer security
  • Deploying shared printers
  • Enabling Branch Office Direct Printing
  • Creating a printer pool
  • Reporting on printer usage

Introduction


Printing is a feature that has been incorporated into Windows operating systems, and has evolved over the years. Printing in Windows Server 2016 has not changed much from earlier versions, and provides you with the ability to create print servers that you can share with users in your organization.

With Windows printing, the physical device that renders output onto paper is a print device. A printer is, in effect, the queue for one or more print devices. A print server can support multiple printers (as can individual client workstations). The print device has a driver that converts your documents to the printed form on a given print device. Some drivers come with Windows—others you need to obtain from the printer vendor.

You use both the printers—that is, the printing device and printer port—when you create a new printer on your print server. In many organizations, printers are often stand-alone devices with just a TCP/IP connection. You can also connect a print device to your...

Installing and sharing printers


The first step in creating a print server for your organization involves installing the print server feature, then installing printer ports and printer drivers, and finally creating printers.

Getting ready

In this recipe, you are going to set up a print server, PSRV, and then set up a TCP/IP printer for the Sales Group. This process involves installing the print server feature in Windows Server 2016. Then you create a port for a new printer and install the driver for the Sales Group group's new printer, an NEC Color MultiWriter. Finally, you create and then share the printer. Once complete, you can review what you have accomplished.

How to do it...

  1. Install the Print-Server feature on PSRV, along with the print management tools:
Install-WindowsFeature -Name Print-Server,                                RSAT-Print-Services
  1. Add a PrinterPort for a new printer:
Add-PrinterPort -Name Sales_Color `       -PrinterHostAddress 10.10.10.61
  1. Add a PrinterDriver for this printer...

Publishing a printer


Once you create and share a printer, as shown in the previous recipe, you can additionally publish it to the Active Directory. When you publish a printer, you can also specify a location for the printer that enables your users to search for published printers based on location and capabilities. End users can search AD to find printers and to find the printers near to them. In this recipe, you publish the printer you created in the previous recipe and examine the results.

Getting ready

In this recipe, you publish the printer that you created in the preceding recipe, Installing and sharing printers.

How to do it...

  1. Get the printer to publish and store the returned object in $Printer:
$Printer = Get-Printer -Name SGCP1
  1. Observe the publication status:
$Printer | Format-Table -Property Name, Published
  1. Publish the printer to AD:
$Printer | Set-Printer -Published $true `                         -Location '10th floor 10E4'
  1. View the updated publication status:
Get-Printer -Name SGCP1 |...

Changing the spool directory


By default, Windows uses the folder %SystemRoot%\System32\spool\PRINTERS to store spooled jobs. On our print server, PSRV, this folder is C:\Windows\System32\spool\PRINTERS. In some cases, particularly when your users generate large amounts of printed output, this folder and the volume could become full, which is not a good thing. To help you avoid issues, you can move the default spool directory to a different folder (for example, C:\Spool), or you could move the spool folder to a folder on another volume (for example, E:\Spool).

Getting ready

There are two ways you can change the spool directory. The first way is to use the classes inside the .NET Framework's System.Printing namespace to update the folder name. The second, and probably the simplest, way is to update the registry with the folder to use for spooling. This recipe shows both methods.

How to do it...

First, let's look at how you change the spool folder using the .NET Framework:

  1. Load the System.Printing...

Changing printer drivers


Once you set up a printer, as shown in the recipe Installing and sharing a printer, users can use the printer and its associated driver to print their documents. You may need to change the driver to change the printer model or to update the driver. In the Installing and sharing a printer recipe, you installed an NEC Color MultiWriter Class Driver, which works with many NEC color printers. But suppose you decide to replace this printer with a different printer model from a different vendor, say an HP color laser printer.

In this recipe, you change the driver for the printer. The assumption behind this recipe is that the printer name and printer port (the printer's IP address and port number) remains constant. You might need to change the printer driver for a printer, should you replace an old printer for a newer or different printer (for example, replacing an NEC printer with an HP printer). In this case, the printing device and its driver changes, but everything else...

Printing a test page on a printer


From time to time, you may wish to print a test page on a printer, for example, after changing toner or printer ink, or after changing the print driver (as shown in the Changing printer drivers recipe). In those cases, the test page helps you to ensure that the printer is working properly.

Getting ready

For this recipe, you print a test page on the Sales Group object's LaserJet printer, as updated by the Changing printer drivers recipe.

How to do it...

  1. Get the printer objects from WMI:
$Printers = Get-CimInstance -ClassName 
                  Win32_Printer
  1. Display the number of printers defined:
'{0} Printers defined on this system' `
           -f $Printers.Count
  1. Get the Sales Group printer:
$Printer = $Printers | 
                  Where-Object Name -eq "SGCP1"
  1. Display the printer's details:
$Printer | Format-Table -AutoSize
  1. Print a test page:
Invoke-CimMethod -InputObject $Printer `                   -MethodName PrintTestPage

How it works...

In step 1, you use Get-CimInstance...

Reporting on printer security


In the Windows operating system, all objects secured by the OS have four key properties:

  • The owner
  • The primary group
  • Discretionary Access Control List (DACL)
  • System Access Control List (SACL)

The DACL contains a set of individual permissions, known as Access Control Entries (ACEs), that define a particular permission. Each ACE contains properties that describe the permission, including a trustee (the security principal to whom you are giving this permission), a permission mask (what permission is being allowed or disallowed), and an ACE type (what type is allowed, disallowed). You can find details of the permission masks on the MSDN.

Getting ready

This recipe displays the DACL for the Sales Group printer, SGCP1, created by the Installing and sharing printers recipe and later updated by the Changing printer drivers recipe. You could easily convert this recipe into an advanced function (for example, Get-PrinterSecurity) with a parameter to tell the function which printer...

Modifying printer security


As you saw in the previous recipe, Reporting on printer security, the DACL for a printer defines what access Windows allows to the printer. To change the set of permissions, you need to change the DACL. You could, for example, update the DACL on the Sales Group printer to just allow members of the Sales Group to print on the printer. This recipe updates the DACL to enable the AD Sales Group to print to the Sales Group printer.

Getting ready

Before you can run this recipe, you need to create a group in the AD. In this recipe, you use a group, Sales Group, contained in the Sales OU. To create the OU, the domain local group, do the following:

# Creating the OU and Group$SB = { New-ADOrganizationalUnit -Name 'Sales'                                 -Path 'DC=Reskit,DC=Org'        New-ADGroup -Name 'Sales Group'                    -Path 'OU=Sales,DC=Reskit,DC=Org'                    -GroupScope DomainLocal}Invoke-Command -ComputerName DC1 -ScriptBlock $SB

How to do it.....

Deploying shared printers


Traditionally, you used scripting to deploy printers. With this method, you create a logon or startup script and deploy this logon script via Group Policies. When machines start up or users log on, the logon script automatically sets up printers.

Once you have set up a shared printer, such as the shared Sales Group color printer, as shown in this chapter, you can deploy it. There are several ways to automate local client printer deployment, including using PowerShell, WMI, the Printui.dll utility, and the Wscript.Network COM object. All of these methods have been in use for a long time and are quite efficient, although PowerShell is the preferred way, naturally.

Getting ready

To deploy a printer to a client, you first need a client computer system. Our demo lab includes a Windows 10 Enterprise client (SG-CL1), which we use in this recipe. To test this recipe, you need the client computer, the print server (PSVR), and the domain controller (DC1).

Once you create the...

Enabling Branch Office Direct Printing


Branch Office Direct Printing (BODP) is a feature introduced in Windows Server 2012 that is designed to reduce print traffic across your WAN. With BODP, a user in a branch office sends the print job directly to a branch office printer. There is no need to send the print job from the client to a centralized print server and then back to the branch office printer. Print jobs can be quite large, so this can improve printing and reduce print job-related WAN traffic.

Getting ready

This recipe needs a second shared printer, SGBRCP1, set up as follows:

    $PtrName = 'Sales_Branch_Color'
    $PtrIP = '172.16.1.61'
    $DrvName = 'NEC Color MultiWriter Class Driver'
    Add-PrinterPort -Name $ptrname `
                    -PrinterHostAddress $PtrIP
    Add-PrinterDriver -Name $DrvName `
                      -PrinterEnvironment 'Windows x64'
    Add-Printer -Name 'SGBRCP1' `
                -DriverName $DrvName `
                -Portname 'Sales_Branch_Color'...

Creating a printer pool


On Windows, a printer pool is a single named printer with two or more print devices (printer ports). Windows sends a given print job to any of the printers in the pool. This feature is very useful in environments where users do a lot of printing and need the speed that additional printers can provide, without having to ask the user to choose which specific print device to use.

There are no PowerShell cmdlets to enable you to create a printer pool. Older printer features—the use of PrintUI.DLL and RunDLL32, which have been features in Windows for several versions. These tools are another example of making use of console applications where you need them.

Getting ready

You run this recipe on the PSRV. Additionally, this recipe assumes you have created the printer, as per the Install and share printers recipe.

How to do it...

  1. Add a new port for this printer:
   Add-PrinterPort -Name Sales_Color2  `                   -PrinterHostAddress   10.10.10.62
  1. Create a printer pool for...

Reporting on printer usage


Printer usage information is useful for capacity planning, and possibly budgeting, for your shared printers. By default, printer usage details are unmonitored, but you can turn logging on (and off!). When you enable printer usage monitoring, the Windows Spooler service writes a record to the event log for each print job containing critical usage information.

This recipe shows you how to turn on printer usage reporting and shows how to create a function to return printer usage information. This recipe creates a function that returns printer usage information as objects. Objects are easier as they enable you to process the output specifically for your environment-for example, counting the total number of pages printed, reporting on who is using the printer, and so on.

Getting ready

Run this recipe on PSRV where you have already set up a printer. This recipe assumes you have set up a printer. You also need to use the printer a bit to generate some event log entries on...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Windows Server 2016 Automation with PowerShell Cookbook - Second Edition
Published in: Sep 2017Publisher: ISBN-13: 9781787122048
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad