远程获取默认打印机

编程入门 行业动态 更新时间:2024-10-23 22:37:40
本文介绍了远程获取默认打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Windows 7:

cscript C:\Windows\System32\Printing_Admin_Scripts\zh-CN\prnmngr .vbs -g

Windows XP:

cscript C:\windows\system32\prnmngr.vbs -g

这些将获取当前系统的默认打印机。我想知道是否可以在计算机上运行此方法,以通过计算机名称获取远程计算机的默认打印机?

我尝试运行:

psexec \\c78572 -i -d cscript C:\Windows\System32\Printing_Admin_Scripts\zh-CN\prnmngr .vbs -g

它似乎正在运行..但我只在快速弹出窗口中看到结果cmd线窗口在远程计算机上而不在我的计算机上。我最后看到的是:

cscript开始于进程ID568。

在powershell中 gwmi win32_printer -computername c78572 可以工作..但我不知道如何排序

EDIT 12/20/13我试图将其与显示所有打印机和默认打印机结合使用,但我无法获得它工作:

而(1){ $ tag1 =读主机'输入标签#或Q退出' if($ tag1-eq Q){中断; } cls sc.exe tag $ tag1启动RemoteRegistry; cls start-sleeps -seconds 2 cls $ OSInfo = get-wmiobject -class win32_operatingsystem-计算机名$ tag1; $ OSInfo |格式表-属性@ {Name = OS Name; Expression = {$$。Caption}},@ {Name = System Boot Time; Expression = {$ _。ConvertToDateTime($ _。LastBootUpTime)}}-自动尺寸; gwmi win32_printer-计算机名$ tag1 | ft-属性@ {Name =打印机名称; Expression = {$$。Name}} -AutoSize; $ Computer = $ tag1 $ Reg = [Microsoft.Win32.RegistryKey] :: OpenRemoteBaseKey('currentuser',$ Computer) $ RegKey = $ Reg.OpenSubKey( '软件\Microsoft\Windows NT\CurrentVersion\Windows') $ DefaultPrinter = $ RegKey.GetValue( Device) $ DefaultPrinter | ConvertFrom-Csv-标题名称,提供者,顺序|选择名称 #替代方法:Get-WmiObject win32_printer -computername c60311

}

解决方案

您可以使用wmi32_printer来获取默认值。下面是代码:

$ AllPrinters = gwmi win32_printer -computername c78572 $ DefaultPrinter = $ AllPrinters |其中{$ _。Default -eq $ true}

这将返回所有本地连接的打印机。如果要获取网络连接打印机的列表(如下文Aaron所述),则会遇到一些问题。上面的脚本不起作用,因为WMI在本地计算机上运行,​​而不是在用户级别上运行。经过大量研究,获取此信息的一种方法是运行登录脚本,因为基本上没有其他方法可以远程使用WMI来获取已登录的用户信息。

如果我们不能使用WMI,该怎么做?使用后门。所有相关信息都存储在注册表中。输出的结果可能并不漂亮,但是它将为您提供我们所需的所有信息。我们只关心3个关键位置:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print \打印机

其中包含所有本地安装打印机。忘了它,使用 gwmi win32_printer 命令获取此列表。

HKEY_CURRENT_USER\打印机\设置

这包含所有当前已登录用户已安装打印机。它没有默认的打印机信息。

HKEY_CURRENT_USER\软件\Microsoft\Windows NT\CurrentVersion\Windows \设备

这是获取当前登录的用户安装默认值的地方>打印机。即,这是Aaron专门寻找的。

因此,我们可以使用PowerShell连接到远程注册表,并使用以下命令读取当前登录的用户的默认打印机。脚本:

$ Computer = c78572 $ Reg = [Microsoft.Win32.RegistryKey] :: OpenRemoteBaseKey( 'currentuser',$ Computer) $ RegKey = $ Reg.OpenSubKey('软件\Microsoft\Windows NT\CurrentVersion\Windows') $ DefaultPrinter = $ RegKey.GetValue( Device ) $ DefaultPrinter | ConvertFrom-Csv-标题名称,提供者,顺序|选择名称

----编辑-获取所有打印机的列表----

要列出远程计算机上的所有打印机:

$ Computer = c78572 #获取本地打印机: $ Printers = @(Get-WmiObject win32_printer -computername $ Computer |选择名称) #获取网络打印机列表: $ Reg = [Microsoft.Win32.RegistryKey] :: OpenRemoteBaseKey('currentuser',$ Computer) $ RegKey = $ Reg.OpenSubKey('Printers\Settings') $打印机+ = @($ RegKey.GetValueNames()) #打印机的输出列表写入输出$ Printers | ft-属性@ {Name =打印机名称; Expression = {$ _。Name}} -AutoSize #获取默认打印机 $ Reg = [Microsoft.Win32 .RegistryKey] ::: OpenRemoteBaseKey('currentuser',$ Computer) $ RegKey = $ Reg.OpenSubKey('Software\Microsoft\Windows NT\CurrentVersion\Windows') $ DefaultPrinter = $ RegKey.GetValue( Device) #输出默认打印机写入输出$ DefaultPrinter | ConvertFrom-Csv-标题名称,提供者,顺序|选择名称| ft-属性@ {Name =默认打印机名称; Expression = {$ _。Name}} -AutoSize

Windows 7:

cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -g

Windows XP:

cscript C:\windows\system32\prnmngr.vbs -g

These will get the default printer of the current system. I was wondering if there is a way to run this on my computer to get the default printer of a remote computer by computer name?

I tried running:

psexec \\c78572 -i -d cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -g

And it appears to run.. but I only see the results in a quick popup cmd line window on the remote computer and not on mine. All I see on my end is:

cscript started with process ID 568.

In powershell gwmi win32_printer -computername c78572 works.. but I don't know how to sort it to show me the default printer.

EDIT 12/20/13 I am trying to combine it with a show all printers and the default but I can't get it to work:

while (1) { $tag1 = Read-Host 'Enter tag # or Q to quit' if ($tag1 -eq "Q") { break; } cls sc.exe \\$tag1 start RemoteRegistry; cls start-sleep -seconds 2 cls $OSInfo = get-wmiobject -class win32_operatingsystem -computername $tag1; $OSInfo | Format-Table -Property @{Name="OS Name";Expression={$_.Caption}},@{Name="System Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}} -AutoSize; gwmi win32_printer -computername $tag1 | ft -Property @{Name="Printer Name";Expression={$_.Name}} -AutoSize; $Computer = $tag1 $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('currentuser', $Computer) $RegKey= $Reg.OpenSubKey('Software\Microsoft\Windows NT\CurrentVersion\Windows') $DefaultPrinter = $RegKey.GetValue("Device") $DefaultPrinter | ConvertFrom-Csv -Header Name, Provider, Order| Select Name # Alt method: Get-WmiObject win32_printer -computername c60311

}

解决方案

You can use wmi32_printer to get the default. Here is the code:

$AllPrinters = gwmi win32_printer -computername c78572 $DefaultPrinter = $AllPrinters | where {$_.Default -eq $true}

This will return all locally attached printers. If you want to get a list of network attached printers (as Aaron commented below), you run into a little bit of an issue. The above script doesn't work because WMI operates on the local machine, and not on the user level. After much research, one way of getting this information is to have a log on script that runs, because there is essentially no other way of remotely using WMI to get the logged in user's information.

How to really do it if we can't use WMI? Use the back door. All the pertinent information is stored in the registry. The output may not be pretty, but it will give you all the information that we need. We are only concerned about 3 key locations:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers

This contains all Locally Installed printers. Forget about it, use the gwmi win32_printer command to get this list.

HKEY_CURRENT_USER\Printers\Settings

This contains all the Currently logged in User Installed printers. It does not have the default printer information.

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device

This is where to get the Currently logged in User Installed Default printer. i.e. This is what Aaron is specifically looking for.

So, we can use PowerShell to connect to the remote registry, and read the currently logged in user's default printer with the following script:

$Computer = "c78572" $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('currentuser', $Computer) $RegKey= $Reg.OpenSubKey('Software\Microsoft\Windows NT\CurrentVersion\Windows') $DefaultPrinter = $RegKey.GetValue("Device") $DefaultPrinter | ConvertFrom-Csv -Header Name, Provider, Order| Select Name

----EDIT - to get a list of all printers----

To list all printers on the remote computer:

$Computer = "c78572" #Get Local Printers: $Printers = @(Get-WmiObject win32_printer -computername $Computer | Select Name) #Get List of Network Printers: $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('currentuser', $Computer) $RegKey= $Reg.OpenSubKey('Printers\Settings') $Printers += @($RegKey.GetValueNames()) #Output List of Printers Write-Output $Printers | ft -Property @{Name="Printer Name";Expression={$_.Name}} -AutoSize #Get Default Printer $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('currentuser', $Computer) $RegKey= $Reg.OpenSubKey('Software\Microsoft\Windows NT\CurrentVersion\Windows') $DefaultPrinter = $RegKey.GetValue("Device") #Output the Default Printer Write-Output $DefaultPrinter | ConvertFrom-Csv -Header Name, Provider, Order| Select Name | ft -Property @{Name="Default Printer Name";Expression={$_.Name}} -AutoSize

更多推荐

远程获取默认打印机

本文发布于:2023-06-05 19:14:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/526469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:打印机

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!