When I run the script below I get an error that the first printer does not exist. This is true, the first printer is not connected, so the output gives me this: "c:\temp\rmprintlong.vbs(4, 1) WSHNetwork.RemoveNetworkDrive: This network connection does not exist." and then the script dies...this is the issue: the script die开发者_运维知识库s!!! I still need it to remove the second printer in this script! If it doesn't find one of the printers in the script, I still need it to resume with the next remove printer command! Here is the code I am using:
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)
Do I simply add "On Error Resume Next" under the first line of the code..?
That depends on the quality requirements of your script. If it doesn't matter whether it succeeds or fails, put the OERN in the first line (and meet the standards of the Scripting Guys).
A compromise/minimal impact/pythonic variation of your script:
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
This would restrict the "I don't care" section to one single operation.
精彩评论