views:

797

answers:

5

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.

A: 

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

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

vitaly.v.ch
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
try HKEY_CURRENT_USER\Printers\Connections
Remko
+1  A: 

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

Get-WMIObject Win32_Printer

EBGreen
This works for TS sessions as well. I get back drivers with names like "<printer> on <server> (from <clientmachine>) in session <sessionId>". 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
This doesn't work in our construct since the printers aren't installed on the Terminal Server. I updated the question.
VVS
A: 

From here: http://www.microsoft.com/technet/scriptcenter/guide/sas%5Fprn%5Ftart.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
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
A: 

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
+1  A: 

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

related questions