![](/img/trans.png)
[英]Powershell: Trouble getting a list of network printers from a remote computer and installing those printers on a new computer
[英]Powershell List Network Printers on Remote PC
我正在嘗試對 GPO 進行故障排除以部署打印機,我需要查看當前登錄用戶的遠程計算機上有哪些網絡打印機。 當我這樣做時
Get-WMIObject Win32_Printer -ComputerName PCNAME
我得到了本地安裝的打印機列表,當我嘗試這個時
Get-WMIObject Win32_Printer -ComputerName PCNAME | where{$_.Name -like “*\\*”} | select sharename,name
我什么也得不到。 有什么幫助嗎? 我正在使用 PowerShell 4.0,因此Get-Printer
不起作用。
我在另一個線程中大量借用了 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…')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.