Author Posts

December 10, 2014 at 2:52 pm

Hello. I've been searching all over and have found some resources that should have yielded me the results I was looking for, but haven't.

I am trying to determine the default printer that is installed for a remote user. In the following script, I am pointing the script to a remote PC (where the user is logged in), grabbing the SID for the user and then pointing the script at the registry key to get the default printer from that SID. It fails to do so.

[b]$username = "user123"
$domain = "mydomainhere"
$Computer = "testcomputer"
$SID = [Get-WmiObject -Class Win32_UserAccount -Filter "Domain = '$domain' AND Name = '$username'"].SID
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey['currentuser', $Computer]
$RegKey = $Reg.OpenSubKey['$SID\Software\Microsoft\Windows NT\CurrentVersion\Windows']
$DefaultPrinter = $RegKey.GetValue["Device"]
Write-Host $DefaultPrinter[/b]

Getting the error:

[b]You cannot call a method on a null-valued expression.
At C:\Users\cverhage\Desktop\Untitled6.ps1:8 char:35
+ $DefaultPrinter = $RegKey.GetValue < <<< ["Device"] + CategoryInfo : InvalidOperation: [GetValue:String] [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull[/b] I"ve also tried something like this: [b]$AllPrinters = gwmi win32_printer -computername testcomputer $DefaultPrinter = $AllPrinters | where {$_.Default -eq $true} Write-Host $DefaultPrinter[/b] This works when I run it from my PC, against my PC but does not give me a result when I run it against another PC. Any suggestions on how to do this? Resources that I"ve used: http://stackoverflow.com/questions/20639541/get-default-printer-remotely
http://powershell.com/cs/forums/p/14466/28166.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2004/11/17/hey-scripting-guy-is-there-any-way-to-determine-the-default-printer-on-a-computer.aspx
https://social.technet.microsoft.com/Forums/windowsserver/en-US/14f33784-09a0-49be-8036-73921181fa3c/microsoftwin32registrykeyopenremotebasekey?forum=winserverpowershell

December 10, 2014 at 3:15 pm

Hi Casey,

1. You need to open the Users key on the remote computer instead of the CurrentUser key.

$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $Computer)

2. The single quotes need to be replaced with double quotes for PowerShell to expand the $SID variable to open the sub key.

$RegKey = $Reg.OpenSubKey("$SID\Software\Microsoft\Windows NT\CurrentVersion\Windows")

Full script:

$username = "user123"
$domain = "mydomainhere"
$Computer = "testcomputer"
$SID = (Get-WmiObject -Class Win32_UserAccount -Filter "Domain = '$domain' AND Name = '$username'").SID
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $Computer)
$RegKey = $Reg.OpenSubKey("$SID\Software\Microsoft\Windows NT\CurrentVersion\Windows")
$DefaultPrinter = $RegKey.GetValue('Device')
$DefaultPrinter

Best,
Daniel

December 10, 2014 at 3:39 pm

This works when I run it against my machine with my user. When I run this against another machine with another user I get the following:

[b]Exception calling "OpenRemoteBaseKey" with "2" argument[s]: "The network path was not found.
"
At C:\Users\cverhage\Desktop\Untitled6.ps1:14 char:56
+ $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey <<<< ['Users', $Computer] + CategoryInfo : NotSpecified: [:] [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException You cannot call a method on a null-valued expression. At C:\Users\cverhage\Desktop\Untitled6.ps1:15 char:26 + $RegKey = $Reg.OpenSubKey <<<< ["$SID\Software\Microsoft\Windows NT\CurrentVersion\Windows"] + CategoryInfo : InvalidOperation: [OpenSubKey:String] [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At C:\Users\cverhage\Desktop\Untitled6.ps1:16 char:35 + $DefaultPrinter = $RegKey.GetValue <<<< ['Device'] + CategoryInfo : InvalidOperation: [GetValue:String] [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull[/b]