Remote Printers In Powershell

Welcome Forums General PowerShell Q&A Remote Printers In Powershell

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
4 years, 1 month ago.

  • Author
    Posts
  • #21067

    Participant
    Points: 0
    Rank: Member

    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.

    $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

    Getting the error:

    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

  • #21068

    Moderator
    Points: 24
    Team Member
    Rank: Member

    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

  • #21070

    Participant
    Points: 0
    Rank: Member

    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:

    Exception calling "OpenRemoteBaseKey" with "2" argument: "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]

The topic ‘Remote Printers In Powershell’ is closed to new replies.