首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell列出远程PC上的网络打印机

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

Stack Overflow用户
提问于 2016-12-16 23:05:54
回答 1查看 10.2K关注 0票数 1

我正在尝试对GPO进行故障排除以部署打印机,并且我需要查看当前登录用户的远程计算机上有哪些网络打印机。当我这样做的时候

代码语言:javascript
复制
Get-WMIObject Win32_Printer -ComputerName PCNAME

我得到了本地安装的打印机的列表,当我尝试执行此操作时

代码语言:javascript
复制
  Get-WMIObject Win32_Printer -ComputerName PCNAME | where{$_.Name -like “*\\*”} | select sharename,name

我什么也得不到。有什么帮助吗?我使用的是PowerShell 4.0,所以Get-Printer无法工作。

EN

回答 1

Stack Overflow用户

发布于 2020-04-07 05:07:50

我在另一个线程中大量借鉴了tukan的代码(谢谢...该代码弥合了一个我还无法弥合的差距),并根据我的目的对其进行了修改。

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

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

代码语言:javascript
复制
#- 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…')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41187221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档