标题: [系统相关] 怎么用批处理脚本自动匹配打印机的端口? [打印本页]
作者: du888111888 时间: 2022-8-2 14:52 标题: 怎么用批处理脚本自动匹配打印机的端口?
场景这样的,每次给客户安装了打印机驱动并设置了端口,但是他们员工有时候会为了充电,拔掉打印机的USB线`然后有时候就不是插在原来的端口上了`
那么这个时候就会跑来反映打印机无法使用,然后每次都要远程去给客户设置打印机端口
有没有办法用脚本自动匹配打印机端口并设置好呢?
感激不尽
作者: flashercs 时间: 2022-8-3 15:04
- <?xml version="1.0" encoding="utf-8"?>
- <package xmlns="http://schemas.microsoft.com/WindowsScriptHost">
- <job id="job1">
- <script language="VBScript">
- <![CDATA[
- '
- Option Explicit
- RunasAdmin
-
- On Error Resume Next
- Const wbemFlagReturnImmediately = &h10
- Const wbemFlagForwardOnly = &h20
- Dim objWMIService,objPrinter,strUsbPort,strOut
- strOut = ""
- Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
- For Each objPrinter In objWMIService.ExecQuery("Select * From Win32_Printer Where PNPDeviceID LIKE '%&USB___'", "WQL", wbemFlagReturnImmediately OR wbemFlagForwardOnly)
- strUsbPort = Right(objPrinter.PNPDeviceID,6)
- strOut = strOut & "打印机:" & objPrinter.Name & vbNewLine
- strOut = strOut & "原端口:" & objPrinter.PortName & vbNewLine
- If objPrinter.PortName <> strUsbPort Then
- objPrinter.PortName = strUsbPort
- strOut = strOut & "新端口:" & strUsbPort & vbNewLine
- Else
- strOut = strOut & "未修改" & vbNewLine
- End If
- strOut = strOut & vbnewline
- Next
- If objPrinter Is Nothing Then
- strOut = strOut & "未找到USB打印机."
- End If
- MsgBox strOut, vbOKOnly Or vbInformation Or vbSystemModal, "打印机"
-
-
- Sub RunasScriptHost(strWSH)
- ' runas cscript.exe or wscript.exe
- On Error Resume Next
- If IsNull(strWSH) Or Not (StrComp(strWSH,"cscript.exe",vbTextCompare) = 0 Or StrComp(strWSH,"wscript.exe",vbTextCompare) = 0) Then
- Exit Sub
- End If
- Dim fso
- Set fso = CreateObject("Scripting.FileSystemObject")
- If StrComp(fso.GetFileName(WScript.FullName),strWSH,vbTextCompare) <> 0 Then
- Dim str,arg,shell
- Set shell = CreateObject("Shell.Application")
- str = ""
- For Each arg In WScript.Arguments
- str = str & " """ & arg & """"
- Next
- shell.ShellExecute strWSH,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "open", 1
- Set shell = Nothing
- Set fso = Nothing
- WScript.Quit
- Else
- Set fso = Nothing
- End If
- End Sub
- Sub RunasAdmin()
- On Error Resume Next
- If Not IsVista(".") Then Exit Sub
- Dim wshell
- Set wshell = CreateObject("WScript.Shell")
- wshell.RegRead "HKEY_USERS\S-1-5-19\Environment\TEMP"
- If Err.Number <> 0 Then
- Dim str,arg,shell
- str = ""
- Set shell = CreateObject("Shell.Application")
- For Each arg In WScript.Arguments
- str = str & " """ & arg & """"
- Next
- shell.ShellExecute WScript.FullName,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "runas", 1
- Set shell = Nothing
- Set wshell = Nothing
- WScript.Quit
- Else
- Set wshell = Nothing
- End If
- End Sub
-
- Function IsVista(strComputer)
- On Error Resume Next
- IsVista = False
- Dim objWMIService, colOperationSystems, objOperationSystem
- Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
- Set colOperationSystems = objWMIService.ExecQuery("Select Version from Win32_OperatingSystem")
- For Each objOperationSystem In colOperationSystems
- If CInt(Left(objOperationSystem.Version, InStr(1,objOperationSystem.Version,".") - 1)) > 5 Then
- IsVista = True
- Exit For
- End If
- Next
- Set colOperationSystems = Nothing
- Set objWMIService = Nothing
- End Function
-
- Sub ShowError(lngID)
- If Err.Number <> 0 Then
- WSH.Echo CStr(lngID) & ":Err # " & Err.Number & vbNewLine & _
- "Description: " & Err.Description & vbnewline & _
- "Source: " & Err.Source
- Err.Clear
- End If
- End Sub
- ]]>
- </script>
- </job>
- </package>
复制代码
保存为 .wsf文件; utf-8编码
作者: semiuel 时间: 2022-8-3 20:09
保存了,正好用得到。
作者: du888111888 时间: 2022-8-8 16:55
回复 2# flashercs
大佬 执行后提示没有找到打印机呢?
作者: du888111888 时间: 2022-8-9 08:25
回复 2# flashercs
我先用打印机连接USB001然后可以打印``然后拔掉usb线连接到USB002端口,然后使用您的脚本,提示未找到USB打印机
作者: flashercs 时间: 2022-8-9 09:51
cmd窗口输入如下命令,把结果发出来.你看看连接不同端口的结果的差异.- wmic printer get DeviceID,Name,PNPDeviceID,Caption,Description /value
复制代码
作者: du888111888 时间: 2022-8-9 11:01
回复 6# flashercs
插在USB001和插在USB002输出都是下面这样
Caption=POS-58
Description=
DeviceID=POS-58
Name=POS-58
PNPDeviceID=
作者: du888111888 时间: 2022-8-9 11:09
回复 6# flashercs
电脑系统是win10的
作者: flashercs 时间: 2022-8-9 12:12
回复 8# du888111888
这就不知道了.问别人吧.
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |