Link to home
Start Free TrialLog in
Avatar of vexious2007

asked on

Help Modifying a Simple VBScript

I need help adding a basic feature/function to this fully functioning script. As of right now it will delete any printers connected to \\OLDserver1 and log it.

I need an exception added to it. I need it to query \\NEWprintserver1 beforehand, store the list of those printers in memory ONLY BY the printers NAME and not delete any \\oldserver1 printer connection that has the same name as a printer on \\NEWprintserver1.

Also if someone can modify it so everytime it's run it doesn't create a new .txt log file, but appends to the existing.

Reason: We want to do a cleanup of old printer connections of decommissioned printers but not delete printers we will soon migrate.
Option Explicit

' Text File Constants
Const cForReading = 1, cForWriting = 2, cForAppending = 8

Dim objNetwork
Dim objPrinters, ix
Dim sPrinter, sUNCPath, sRemove
Dim sDateStamp, sLogPath, oLogFile, oFSO

Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections

' Open file for logging this session, named with date / time stamp (LOG_YYYYMMDD_HHMMSS.TXT)
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDateStamp = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & "_" & Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2)
sLogPath = "c:\temp\log_" & sDateStamp & ".txt" 
Set oLogfile = oFSO.OpenTextFile(sLogPath, cForWriting, True)

' ### Delete all currently installed network printers
For ix= 0 to objPrinters.Count - 1 Step 2
   sPrinter = objPrinters.Item(ix)
   sUNCPath = objPrinters.Item(ix+1)

   If InStr(1, sPrinter, "\\vz1prntserver1", 1) > 0 Then
      sRemove = sPrinter
   ElseIf InStr(1, sUNCPath, "\\vz1prntserver1", 1) > 0 Then
      sRemove = sUNCPath
      sRemove = ""
   End If

   ' Try to remove printer, log results
   If sRemove <> "" then
      On Error Resume Next
      objNetwork.RemovePrinterConnection sRemove, True, True
      If Err = 0 Then
         oLogFile.WriteLine FormatDateTime(Now()) & " - Deleted printer [" & sRemove & "]"
         oLogFile.WriteLine FormatDateTime(Now()) & " - Error [" & Err.Description & "] deleting printer [" & sRemove & "]"
         Wscript.Echo "Error [" & Err.Description & "] deleting printer [" & sRemove & "]"
      End If
      On Error GoTo 0
   End if

' CLose log file

Open in new window

Avatar of dlwyatt82
Flag of Canada image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vexious2007


I've requested that this question be deleted for the following reason:

Not sufficent help.
I responded to his question with code, and he gave no response other than to request a deletion. The only thing insufficient here is feedback on the solution provided.