如何解决使用PowerShell获取远程用户打印机
我看到几条没有答案的帖子,我试图使远程用户打印机网络和本地打印机都没有成功,我需要在此代码中进行修复才能使其正常工作?
write-output "Get remote user network printers"
$Computer = read-host -prompt "Enter Hostname\IP Address"
$UserName = read-host 'Enter desired User'
$Domain = "mydomain"
$DomainCredential = Get-Credential "mydomain\***adm"
$Service = "RemoteRegistry"
$RemoteRegistry = Get-CimInstance -Class Win32_Service -ComputerName $Computer -Filter 'Name = "$Service"'
Set-Service -Name $Service -ComputerName $Computer -StartupType Automatic
Start-Service -InputObject (Get-Service -Name $Service -ComputerName $Computer)
$UserSID = ((New-Object System.Security.Principal.NTAccount($UserName)).Translate([System.Security.Principal.SecurityIdentifier])).Value
$regkey = Get-ChildItem "Registry::HKEY_USERS\$UserSID\printers\connections" | Select Name
$Printers = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regkey,$Computer)
# If they have printers
If ($Printers)
{
# Create an array to hold their printers
$PrinterNames = @()
}
Foreach ($Printer in $Printers)
{
# Get a list of their printers
$PrinterNames += $Printer.ToString().Substring($Printer.LastIndexOf("\") + 1).Replace(",","\")
}
# If they have a default printer
If (Test-Path -Path "Registry::HKEY_USERS\$UserSID\Software\Microsoft\Windows NT\CurrentVersion\Windows")
{
# Get the default printer string
$Default = Get-ChildItem "Registry::HKEY_USERS\$UserSID\Software\Microsoft\Windows NT\CurrentVersion" `
| Get-ItemProperty | Where-Object {$_.Device} | Select-Object -ExpandProperty Device
# Trim it to get the value we want
$Default = $Default.SubString(0,$Default.IndexOf(","))
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。