使用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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...