2

I am looking for a way to see what printers a specific user has mapped into his or her TS session.

How can I achieve this with WMI (via PowerShell) or VB-Script? Is there a built-in way I'm not aware of?

EDIT: In our construct mapping of local printers by the RDP-Client is disabled. Users get their printers created during login via VBS-Script and deleted during logoff.

So there's no printers installed directly on our TS server and querying the Win32_Printers WMI class returns nothing. The printers are installed on a dedicated print server. Querying the printers on that server returns ALL printers and not the one mapped for a single user.

VVS
  • 19,405
  • 5
  • 46
  • 65

6 Answers6

2

Thanks to Remko's comment I was put into the right direction and finally made a script that did what I needed.

Basically the script determines the SID of the user and looks in the user's registry hive (HKEY_USERS\$sid\Printers\Connections) for the created printers.

Here's the quick and dirty powershell script:

$server = 'servername'
$userName = 'username'

$regHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $server)
$regProfileList = $regHKLM.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")

foreach ($sid in $regProfileList.GetSubKeyNames())
{
    $profileImagePath = $regProfileList.OpenSubKey($sid).GetValue("ProfileImagePath")
    if ($profileImagePath.EndsWith("\$userName"))
    {
        $regHKU = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("Users", $server)
        $regUser = $regHKU.OpenSubKey("$sid\Printers\Connections")
        foreach ($printer in $regUser.GetSubKeyNames())
        {
            $printer.Replace(",", "\")  # backslashes are replaced with commas, revert that
        }
    }
}
VVS
  • 19,405
  • 5
  • 46
  • 65
1

I can't check in a TS session right now, but this does it normally in powershell:

Get-WMIObject Win32_Printer

EBGreen
  • 36,735
  • 12
  • 65
  • 85
  • This works for TS sessions as well. I get back drivers with names like " on (from ) in session ". They also have a port name of "TSxxx". There may be an object for the relationship to a session, but I couldn't find anything. – JasonMArcher Mar 24 '09 at 21:09
  • This doesn't work in our construct since the printers aren't installed on the Terminal Server. I updated the question. – VVS Mar 25 '09 at 09:10
0

From here: http://www.microsoft.com/technet/scriptcenter/guide/sas_prn_tart.mspx?mfr=true

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Printer")
For Each objPrinter in colInstalledPrinters
 Wscript.Echo "Name: " & objPrinter.Name
 Wscript.Echo "Location: " & objPrinter.Location
Next
seanyboy
  • 5,623
  • 7
  • 43
  • 56
  • Using this method will give you the list of printers, that are installed locally on the server - which is empty. The printers for the user are added to the session via WshNetwork.AddWindowsPrinterConnection and can't be seen by your script. – VVS Jul 20 '10 at 13:28
0

May be You need CUPS based tecnology? It's simple task for cups on any unix but I'm not sure about Windows.

vitaly.v.ch
  • 2,485
  • 4
  • 26
  • 36
0

This did the trick for me unlick seanyboy answer which returns the local printers this script return the network printers a users is connected to, works fine on a Terminal Server \ Citrix session

http://www.geekshangout.com/vbs-script-to-list-the-network-printers-a-user-is-connected-to/

Phil Eddies
  • 208
  • 1
  • 6
  • This might work to get the actual user's network printers, but my intention was to get the list of printers for a specific user. – VVS Apr 06 '11 at 07:21
-1

As far as I understand You can read some field in Registry.

PS: I prefer to use Linux for terminal service ;)

vitaly.v.ch
  • 2,485
  • 4
  • 26
  • 36
  • This doesn't work, as on Remote Desktop, the registry HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices can contain printers which are no longer active. – seanyboy Sep 15 '09 at 08:41
  • 2
    try HKEY_CURRENT_USER\Printers\Connections – Remko Jul 12 '10 at 08:32