VBS删除打印——错误继续下一个



当我运行下面的脚本时,我得到一个错误,第一台打印机不存在。这是真的,第一台打印机没有连接,所以输出给我的是:"c: temp rmprintlong。vbs(4,1) WSHNetwork。RemoveNetworkDrive: This network connection does not exist."然后脚本终止…这就是问题所在:脚本死了!!我仍然需要它来删除这个脚本中的第二台打印机!如果它没有在脚本中找到一个打印机,我仍然需要它继续使用下一个删除打印机命令!下面是我使用的代码:

Set WshNetwork = WScript.CreateObject("WScript.Network")
PrinterPath = "\p1(SW)_HP_LaserJet_8150_Series_PCL"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
PrinterPath = "\p1(NE)_HP_LaserJet_5M"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
Wscript.Quit(1)

我是否只需在代码的第一行下添加"On Error Resume Next"?

这取决于脚本的质量要求。如果成功或失败无关紧要,则将OERN放在第一行(并符合脚本编写人员的标准)。

脚本的折衷/最小影响/python变体:

Set WshNetwork = WScript.CreateObject("WScript.Network")
PrinterPath = "\p1(SW)_HP_LaserJet_8150_Series_PCL"
On Error Resume Next ' don't care if this fails
WshNetwork.RemovePrinterConnection PrinterPath, true, true
On Error GoTo 0 ' open eyes again
PrinterPath = "\p1(NE)_HP_LaserJet_5M"
WshNetwork.RemovePrinterConnection PrinterPath, true, true
Wscript.Quit 1  ' no parameter list () when calling a sub

这将限制"I don't care"部分为单个操作

相关内容

最新更新