I'm trying to write a script that checks which print server users on our network are connecting to. To do that, I'm attempting to check the registry values listed under Printer\Connections. On my local machine the following two methods both work:
1)
Get-ChildItem HKCU:\Printers\Connections
Output:
Hive: HKEY_CURRENT_USER\Printers\Connections
Name Property
----- --------
,,printServer,printerName GuidPrinter : {guid}
Server : \\printserver.domain
Provider : win32spl.dll
LocalConnection : 1
2)
>> Get-ChildItem HKCU:\Printers\Connections | ForEach-Object {Get-ItemProperty $_.pspath}
And the output of that command is:
GuidPrinter : {guid}
Server : \\printServer.domain
Provider : win32spl.dll
LocalConnection : 1
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Printers\Connections\,,printServerName,printerName
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Printers\Connections
PSChildName : ,,printServerName, printerName
PSProvider : Microsoft.PowerShell.Core\Registry
By themselves, neither of these implementations are easily applied to remote machines. My most recent attempt to get the same information shown above off of a remote machine is:
$machine = 'computerName';
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('CurrentUser',$machine);
$regKey = $reg.OpenSubKey("Printers\Connections");
$regKey.GetValueName()
# I also tried the following which I didn't think
# would work, but I was grasping at straws:
# Get-ChildItem $regKey;
The above code returns nothing. It doesn't throw an error but regKey returns an empty string.
Does anyone know of an alternative solution to getting the information I'm looking for or see something wrong in the implementation above?
Thanks.
By no means perfect but I came up with this. In examining its parts there are couple of different ways to get the same thing but this was the first approach where I was able to get tangible results. The function capabilities are lacking but I wanted to showcase the results more than the means.
You can pass individual computer names to the function and it will find the logged in users. They way it figures out who is logged in is based one users with an associated
explorer.exe
process. For each of those usernames check again active directory to get their SID (Get-AdUser
might not be the best way but it is a simple one). Then we use that information to open the remote registry and get the network printer configuration.Some sample output:
There are several potential errors that are not accounted for. So this is definitely subject to improvement over time.
You can also consider other remote registry options that are covered in this question and possibly adapt them to the logic here if what I have does not help you.