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.
' Text File Constants
Const cForReading = 1, cForWriting = 2, cForAppending = 8
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 = ""
' 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 & "]"
On Error GoTo 0
' CLose log file