Powershell列出远程PC上的网络打印机

时间:2016-12-16 15:05:54

标签: powershell printing printers

我正在尝试解决GPO部署打印机问题,我需要查看当前登录用户在远程计算机上的网络打印机。当我这样做时

Get-WMIObject Win32_Printer -ComputerName PCNAME

我得到了本地安装的打印机列表,当我尝试这个时

  Get-WMIObject Win32_Printer -ComputerName PCNAME | where{$_.Name -like “*\\*”} | select sharename,name

我一无所获。有帮助吗?我正在使用PowerShell 4.0,因此Get-Printer无效。

1 个答案:

答案 0 :(得分:0)

我在另一个线程中从tukan的代码中大量借用(感谢...该代码弥合了我尚无法弥合的差距),并对其进行了修改以达到我的目的。

代码泄漏确定了指定远程计算机上的登录用户,然后输出该用户在注册表中列出的打印机在HKU \\ Printers \ Connections下。

作为奖励,我添加了一些额外的行,用于输出用户当前选择的默认打印机。

#- BEGIN FUNCTION -------------------------------------------------------------------------------------------------
Function remote_registry_query($target, $key)
{
   Try {
        $registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("Users", $target)
        ForEach ($sub in $registry.OpenSubKey($key).GetSubKeyNames())
        {
            #This is really the list of printers
            write-output $sub
        }

  } Catch [System.Security.SecurityException] {
        "Registry - access denied $($key)"
  } Catch {
        $_.Exception.Message
  }
}
#- END FUNCTION ---------------------------------------------------------------------------------------------------


##############################
# EXECUTION STARTS HERE
##############################

# Prep variables and get information for the function call


# set the computer name
$computer = "computer_name"


# get the logged-in user of the specified computer
$user = Get-WmiObject –ComputerName $computer –Class Win32_ComputerSystem | Select-Object UserName
$UserName = $user.UserName
write-output " "
write-output " "
write-output "Logged-in user is $UserName"
write-output " "
write-output " "
write-output "Printers are:"
write-output " "

# get that user's AD object
$AdObj = New-Object System.Security.Principal.NTAccount($user.UserName)

# get the SID for the user's AD Object 
$strSID = $AdObj.Translate([System.Security.Principal.SecurityIdentifier])

#remote_registry_query -target $computer -key $root_key
$root_key = "$strSID\\Printers\\Connections"
remote_registry_query -target $computer -key $root_key

# get a handle to the "USERS" hive on the computer
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("Users", $Computer)
$regKey = $reg.OpenSubKey("$strSID\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows")

# read and show the new value from the Registry for verification
$regValue = $regKey.GetValue("Device")
write-output " "
write-output " "
write-output "Default printer is $regValue"
write-output " "
write-output " "
[void](Read-Host 'Press Enter to continue…')