We help IT Professionals succeed at work.
Get Started

Help Modifying a Simple VBScript

442 Views
Last Modified: 2012-05-11
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
   Else
      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 & "]"
      Else
         oLogFile.WriteLine FormatDateTime(Now()) & " - Error [" & Err.Description & "] deleting printer [" & sRemove & "]"
         Wscript.Echo "Error [" & Err.Description & "] deleting printer [" & sRemove & "]"
         Err.Clear
      End If
      On Error GoTo 0
   End if
Next

' CLose log file
oLogFile.Close

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE