Avatar of vexious2007
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

VB ScriptVisual Basic ClassicScripting Languages

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck