Purge Printers from Windows 7 VBS

''''''''''Execute subs CheckArgs CleanupPrinters dim SleepTime ''globals for sleep time in Milliseconds dim quiet ''globals for no dialog dim Msg ''Message string '''''''''''----------------------------------------------------- sub CheckArgs() ''push args into array ReDim args(WScript.Arguments.Count-1) For i = 0 To WScript.Arguments.Count-1 args(i) = WScript.Arguments(i) & "" Next For argCt = 0 to ubound(args) if lcase(trim(args(argCt))) = "\sleep" then argCt=argCt+1 SleepTime = int(args(argCt)) msg = msg & vbCr & "\sleep=" & int(SleepTime) end if if lcase(trim(args(argCt) & "" )) = "\q" or lcase(trim(args(argCt) & "" )) = "\quiet" then quiet=true msg = msg & vbCr & "\quiet=" & quiet End if Next if msg > "" then msg = msg & vbCr end sub '''''''----------------------------------------------------- Sub CleanupPrinters() if SleepTime > 0 then Wscript.Sleep int(SleepTime) ''On Error Resume Next Dim re ''As New regexp Set re = CreateObject("VBScript.RegExp") re.IgnoreCase = True Dim match ''As MatchCollection Dim objprinter ''As SWbemObjectEx Dim strPrnName ''as string ''printer name strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set WSHNetwork = CreateObject("WScript.Network") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer Where Network = TRUE") '''For adding::: WSHNetwork.AddWindowsPrinterConnection "\\USGWV01VAP0002\USGWV01-1FL-HP-DJ-T3500-01" msg = msg & vbCr & "Added:" & vbCr & "USGWV01-1FL-HP-DJ-T3500-01" & vbCr '''For Culling and removing anything that doesn't match REGEX pattern: Dim CT For Each objprinter In colInstalledPrinters ''2018-03-13-re.Pattern = "(?:.*\-1FL){0,1}(?:\-{0,1}(?:ENV|BLUEBEAM PDF|HP|XR|DJ)){1,2}(?:\-{0,1}.\d{0,4}\-){0,1}.*" re.Pattern = ".*\-.*(?:env|blue|hp[-]{0,1}dj\d{0,4})\-.*" ''reg exp to keep - first floor and strPrnName = objprinter.Path_.Keys.Item("DeviceID").Value Set match = re.Execute(UCase(strPrnName)) If match.count = 0 Then ''didn't match to keep CT = CT + 1 If CT = 1 Then msg = msg & vbCr & "Deleted:" & vbCr msg = msg & vbCr & objprinter.Path_.Keys.Item("DeviceID").Value 'Stop objprinter.Delete_ End If Next If msg = "" Then msg = "Nothing matched to delete and/or add" if not quiet then MsgBox msg, vbOK, "Modifications to print list" End Sub

Comments

Popular posts from this blog

Powerpoint countdown and current time in slides VBA

Revit area plans adding new types and references (Gross and rentable)