Tuesday, July 24, 2018

Create a Shared Printer Queue Remotely in PowerShell

There are a couple of ways to create printer port and printer queue with PowerShell, depending on the OS, performance and personal preferences.

The following scripts are creating a shared printer queue on a remote print server.

1. Print Management Cmdlets

If you are with Windows 2012 or later OS, you can use print management cmdlets.

Add-PrinterPort -ComputerName $ServerName -Name $PrinterIP -PrinterHostAddress $PrinterIP  
Add-Printer -ComputerName $ServerName -Name $PrintQName -Shared -ShareName $PrintQName -Comment $PrinterIP -Location $PrintLocation -DriverName $DriverName -PortName $PrinterIP
2. WMI

On Windows Server 2008 R2 or lower, we can use WMI for printer tasks. The good thing with WMI is its simplicity, which takes strings as parameters.

Part one of this section is to create a printer port on the remote server (reference).
$ip = "10.0.0.10"
$port = [WMIClass]"\\$ComputerName\Root\cimv2:Win32_TcpIpPrinterPort"
$port.psbase.scope.options.EnablePrivileges = $true
$newport = $port.CreateInstance()
$newport.name = "$ip"
$newport.Protocol = 1
$newport.HostAddress = $ip
$newport.PortNumber = "9100"
$newport.SnmpEnabled = $false
$newport.Put()
Part two of this section is to create a printer queue using the above-created printer port (reference).
$wmi = ([WMIClass]"\\$ComputerName\Root\cimv2:Win32_Printer")
$Printer = $wmi.CreateInstance()
$Printer.Caption = $PrinterCaption
$Printer.DriverName = $DriverName (e.g. "PCL6 for Univeral Print")
$Printer.PortName = $PrinterPortName
$Printer.DeviceID = $PrinterCaption
$Printer.Network = $True
$Printer.Shared = $True
$Printer.ShareName = $PrinterCaption
$Printer.Put()

3. .NET System.Printing

.NET has a few overload methods and it is not easy to get examples for all of them. (reference)

Currently, I have not found a direct .NET method to create printer port, please use the WMI method above.

InstallPrintQueue() with 11 parameters
Add-Type -AssemblyName System.Printing

[string[]]$PrintPorts = $IPAddress # one single port
$PrintProcessorName = "winprint"
$PrintQueueSeperatorFile = ""
$PrintQueueAttributes = @()
$PrintQueueAttributes += [System.Printing.PrintQueueAttributes]::Shared

[System.Printing.PrintServer]("\\$ComputerName").InstallPrintQueue( `
$PrinterName, $QueueDriver, $PrintPorts, $PrintProcessorName, `
$PrintQueueAttributes, $PrinterName, `
$Comment, $Location, $PrintQueueSeperatorFile,1,0)

Thanks.

No comments:

Post a Comment

Compare Print Queues on Two Print Servers

In an active-active print servers scenario, we would like to make sure all print queues are identical. This means: 1. Print queue names are...