[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[系统相关] 怎么用批处理脚本自动匹配打印机的端口?

场景这样的,每次给客户安装了打印机驱动并设置了端口,但是他们员工有时候会为了充电,拔掉打印机的USB线`然后有时候就不是插在原来的端口上了`
那么这个时候就会跑来反映打印机无法使用,然后每次都要远程去给客户设置打印机端口
有没有办法用脚本自动匹配打印机端口并设置好呢?


感激不尽

回复 8# du888111888


    这就不知道了.问别人吧.
微信:flashercs
QQ:49908356

TOP

回复 6# flashercs


    电脑系统是win10的

TOP

回复 6# flashercs
插在USB001和插在USB002输出都是下面这样


Caption=POS-58
Description=
DeviceID=POS-58
Name=POS-58
PNPDeviceID=

TOP

cmd窗口输入如下命令,把结果发出来.你看看连接不同端口的结果的差异.
  1. wmic printer get DeviceID,Name,PNPDeviceID,Caption,Description /value
复制代码
微信:flashercs
QQ:49908356

TOP

回复 2# flashercs


  我先用打印机连接USB001然后可以打印``然后拔掉usb线连接到USB002端口,然后使用您的脚本,提示未找到USB打印机

TOP

回复 2# flashercs


    大佬  执行后提示没有找到打印机呢?

TOP

保存了,正好用得到。

TOP

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <package xmlns="http://schemas.microsoft.com/WindowsScriptHost">
  3.   <job id="job1">
  4.     <script language="VBScript">
  5.       <![CDATA[
  6. '
  7. Option Explicit
  8. RunasAdmin
  9. On Error Resume Next
  10. Const wbemFlagReturnImmediately = &h10
  11. Const wbemFlagForwardOnly = &h20
  12. Dim objWMIService,objPrinter,strUsbPort,strOut
  13. strOut = ""
  14. Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
  15. For Each objPrinter In objWMIService.ExecQuery("Select * From Win32_Printer Where PNPDeviceID LIKE '%&USB___'", "WQL", wbemFlagReturnImmediately OR wbemFlagForwardOnly)
  16.   strUsbPort = Right(objPrinter.PNPDeviceID,6)
  17.   strOut = strOut & "打印机:" & objPrinter.Name & vbNewLine
  18.   strOut = strOut & "原端口:" & objPrinter.PortName & vbNewLine
  19.   If objPrinter.PortName <> strUsbPort Then
  20.     objPrinter.PortName = strUsbPort
  21.     strOut = strOut & "新端口:" & strUsbPort & vbNewLine
  22.   Else
  23.     strOut = strOut & "未修改" & vbNewLine
  24.   End If
  25.   strOut = strOut & vbnewline
  26. Next
  27. If objPrinter Is Nothing Then
  28.   strOut = strOut & "未找到USB打印机."
  29. End If
  30. MsgBox strOut, vbOKOnly Or vbInformation Or vbSystemModal, "打印机"
  31. Sub RunasScriptHost(strWSH)
  32.   ' runas cscript.exe or wscript.exe
  33.   On Error Resume Next
  34.   If IsNull(strWSH) Or Not (StrComp(strWSH,"cscript.exe",vbTextCompare) = 0 Or StrComp(strWSH,"wscript.exe",vbTextCompare) = 0) Then
  35.     Exit Sub
  36.   End If
  37.   Dim fso
  38.   Set fso = CreateObject("Scripting.FileSystemObject")
  39.   If StrComp(fso.GetFileName(WScript.FullName),strWSH,vbTextCompare) <> 0  Then
  40.     Dim str,arg,shell
  41.     Set shell = CreateObject("Shell.Application")
  42.     str = ""
  43.     For Each arg In WScript.Arguments
  44.       str = str & " """ & arg & """"
  45.     Next
  46.     shell.ShellExecute strWSH,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "open", 1
  47.     Set shell = Nothing
  48.     Set fso = Nothing
  49.     WScript.Quit
  50.   Else
  51.     Set fso = Nothing
  52.   End If
  53. End Sub
  54. Sub RunasAdmin()
  55.   On Error Resume Next
  56.   If Not IsVista(".") Then Exit Sub
  57.   Dim wshell
  58.   Set wshell = CreateObject("WScript.Shell")
  59.   wshell.RegRead "HKEY_USERS\S-1-5-19\Environment\TEMP"
  60.   If Err.Number <> 0 Then
  61.     Dim str,arg,shell
  62.     str = ""
  63.     Set shell = CreateObject("Shell.Application")
  64.     For Each arg In WScript.Arguments
  65.       str = str & " """ & arg & """"
  66.     Next
  67.     shell.ShellExecute WScript.FullName,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "runas", 1
  68.     Set shell = Nothing  
  69.     Set wshell = Nothing
  70.     WScript.Quit
  71.   Else
  72.     Set wshell = Nothing
  73.   End If
  74. End Sub
  75. Function IsVista(strComputer)
  76.   On Error Resume Next
  77.   IsVista = False
  78.   Dim objWMIService, colOperationSystems, objOperationSystem
  79.   Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  80.   Set colOperationSystems = objWMIService.ExecQuery("Select Version from Win32_OperatingSystem")
  81.   For Each objOperationSystem In colOperationSystems
  82.     If CInt(Left(objOperationSystem.Version, InStr(1,objOperationSystem.Version,".") - 1)) > 5 Then
  83.       IsVista = True
  84.       Exit For
  85.     End If
  86.   Next
  87.   Set colOperationSystems = Nothing
  88.   Set objWMIService = Nothing
  89. End Function
  90. Sub ShowError(lngID)
  91.   If Err.Number <> 0 Then
  92.     WSH.Echo CStr(lngID) & ":Err # " & Err.Number & vbNewLine & _
  93.     "Description: " & Err.Description & vbnewline & _
  94.     "Source: " & Err.Source
  95.     Err.Clear
  96.   End If
  97. End Sub
  98. ]]>
  99.     </script>
  100.   </job>
  101. </package>
复制代码
保存为 .wsf文件; utf-8编码
1

评分人数

微信:flashercs
QQ:49908356

TOP

返回列表