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.
It is because the key you are looking for is HKEY Current user, which although is correct changes with each user depending on who is logged in, so when you run it remotely is loading your hive not the users. you would have to get the user to run it, and one of the easiest ways would be to through it into the startup folder of the machine and it will run and report back to you without them knowing.