0

I manage a terminal server in our production environment that serves many different machines that connect under the same generic ID. Using the Cassia library, I am easily able to capture everything I need to know about the remote connections programmatically, but I am as of yet stumped as to how I need to go about applying the printer change to that user. My distinguishing criteria will be the name of the PC that s/he is connecting from. There are 4 machines in particular that I am interested in, and the rest will be ignored.

In a nutshell, I need to capture when these particular PCs log on, and set a default printer for them for the remainder of their session.

At a local level, it is no challenge to change a default printer programmatically. I seem to be struggling with changing the default printer for a user's session on a remote terminal server from another utility server.

Ki4qwq
  • 1
  • 1

2 Answers2

0

The easiest way to do this is create a script that is set to run on login and set the printer via a WMI to Win32_Printer.SetDefaultPrinter.

Here is a example of how to do it in a simple 3 line powershell script

$Printers = Get-WmiObject -Class Win32_Printer
$Printer = $Printers | Where{$_.Name -eq 'Name Of Printer To Use'}
$Printer.SetDefaultPrinter()

Save that in a .ps1 file and set the terminal server policies to run the script on login.

If you don't want to use a fixed name in the script it is easily modifiable to query some other external source, just replace 'Name Of Printer To Use' with a variable that holds the name you want to connect to.

The SetDefaultPrinter method is supported on Windows Vista/Server 2003 and up.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • This is exactly what I've experimented from the C# side, but I haven't done anything with a script. The issue I've had is not setting the printer, it's mapping the set printer action to particular users logged on to the terminal server. This looks promising and I'll give it a go first thing next week. – Ki4qwq Jun 12 '15 at 21:46
0

This sets the default printer for the current user on a Terminal server.

$strPrinter = "\\ITPG-FP01\Kyocera A4 ZW"
$printer = Get-WmiObject -Class Win32_Printer | Where-Object { $_.Name -eq $strPrinter }

if ($printer) {
    $deviceId = $printer.DeviceID
    $network = New-Object -ComObject WScript.Network
    $network.SetDefaultPrinter($deviceId)
}
Smeddeu
  • 23
  • 3