delete the old printer connections using the registry, dont bother with the network.removeprinter method, it is horrid and slow. for that matter when enumerating printers go straight for the registry, if the user has a printer connection which cannot be communicated with it will make your script (logon) flippin slow:
'######### sWAP PRINTERS
Private Sub swapPrintServer(str2Swap)
On Error Resume Next
Dim sPSArray, sPrinter
If blnRegPrinter = False Then
'msgbox "init printers"
Dim strSPRootKey, arrSPSubKeys, refSPRegistry, subSPKey, strSPPrinterMapping
Set enumSPPrinters = WScript.CreateObject("Scripting.Dictionary")
Set refSPRegistry = GetObject("winmgmts:root\default:StdRegProv")
Const HKEY_CURRENT_USER = &H80000001
strSPRootKey = "Printers\Connections"
'Msgbox enumSPPrinters.Count
If refSPRegistry.EnumKey(HKEY_CURRENT_USER, strSPRootKey, arrSPSubKeys) = 0 Then
'init dictionary object of printers
For Each subSPKey In arrSPSubKeys
strSPPrinterMapping = ""
strSPPrinterMapping = Trim(LCase(CStr(subSPKey)))
'Wscript.Echo strSPPrinterMapping
If InStr(strSPPrinterMapping, ",,") = 1 Then
'Wscript.Echo strSPPrinterMapping & " has a ,,"
strSPPrinterMapping = Replace(strSPPrinterMapping, ",", "\")
'Wscript.Echo strSPPrinterMapping & enumSPPrinters.Count
If enumSPPrinters.Exists(strSPPrinterMapping) Then
'already have this one, unlikely
Else
'Wscript.Echo "adding " & strSPPrinterMapping & " to enumPrinters"
enumSPPrinters.Add strSPPrinterMapping, "1"
End If
End If
Next
End If
Set refSPRegistry = Nothing
blnRegPrinter = True
End If
Call LOG_BUFFER("SWAP PRINTER SUB, TRYING TO SWAP/DISCONNECT " & str2Swap, "full", "full")
'aCGC = "\\braps01a&\\172.25.12.254"
sPSArray = Split(str2Swap, "&", -1, 1)
If UBound(sPSArray) = 0 Then
'msgbox "only dlete not readd"
'just want to delete old printers
For Each sPrinter In enumSPPrinters
'wscript.Echo sPrinter & " " & sPSArray(0)
If Left(LCase(sPrinter), Len(sPSArray(0))) = LCase(CStr(sPSArray(0))) Then
'Wscript.Echo "found our server in the string " & LCase(CStr(sPrinter))
Call LOG_BUFFER("ONLY GOING TO REMOVE PRINTER CALLED " & LCase(CStr(sPrinter)), "FULL", "FULL")
WshShell.RegDelete "HKCU\Printers\Connections\" & Replace(LCase(CStr(sPrinter)), "\", ",") & "\"
Call LOG_BUFFER("Err Number = " & Err.Number, "FULL", "FULL")
Call LOG_BUFFER("AFTER REMOVE PRINTER CALL " & LCase(CStr(sPSArray(0))), "FULL", "FULL")
End If
Next
End If
If UBound(sPSArray) = 1 Then
'msgbox "delete and re add"
For i = 0 to enumSPPrinters.Count -1
If Left(LCase(CStr(enumSPPrinters.Item(i))), Len(sPSArray(0))) = LCase(CStr(sPSArray(0))) Then
'msgbox wshPrinters.Item(i)
'msgbox Right(LCase(CStr(wshPrinters.Item(i))), Len(CStr(wshPrinters.Item(i))) - Len(sPSArray(0)) - 1)
Call LOG_BUFFER("GOING TO REMOVE THEN RE-MAP TO NEW SERVER", "FULL", "FULL")
Call LOG_BUFFER("GOING TO REMOVE " & enumSPPrinters.Item(i), "FULL", "FULL")
WshShell.RegDelete "HKCU\Printers\Connections\" & Replace(LCase(CStr(enumSPPrinters.Item(i))), "\", ",") & "\"
Call LOG_BUFFER("AFTER REMOVE PRINTER CALL " & enumSPPrinters.Item(i), "FULL", "FULL")
Call LOG_BUFFER("BEFORE RE-MAP PRINTER CALL TO SERVER " & sPSArray(1), "FULL", "FULL")
waspMach.MapPrinter sPSArray(1) & "\" & Right(LCase(CStr(enumSPPrinters.Item(i))), Len(CStr(enumSPPrinters.Item(i))) - Len(sPSArray(0)) - 1), "False"
Call LOG_BUFFER("AFTER RE-MAP PRINTER CALL TO SERVER " & sPSArray(1), "FULL", "FULL")
End If
Next
End If
End Sub
Public Function MapPrinter(strPrinter, blnDisconnect)
'initilize printers dicObject
On Error Resume Next
If blnMapPrinter = False Then
'msgbox "init printers"
Dim strRootKey, arrSubKeys, refRegistry, subKey, strPrinterMapping
Set enumPrinters = WScript.CreateObject("Scripting.Dictionary")
Set refRegistry = GetObject("winmgmts:root\default:StdRegProv")
Const HKEY_CURRENT_USER = &H80000001
strRootKey = "Printers\Connections"
If refRegistry.EnumKey(HKEY_CURRENT_USER, strRootKey, arrSubKeys) = 0 Then
'init dictionary object of printers
For Each subKey In arrSubKeys
strPrinterMapping = ""
strPrinterMapping = Trim(LCase(CStr(subKey)))
If InStr(strPrinterMapping, ",,") = 1 Then
strPrinterMapping = Replace(strPrinterMapping, ",", "\")
If enumPrinters.Exists(strPrinterMapping) Then
'already have this one, unlikely
Else
'Wscript.Echo "adding " & strPrinterMapping & " to enumPrinters"
enumPrinters.Add strPrinterMapping, "1"
End If
End If
Next
End If
Set refRegistry = Nothing
blnMapPrinter = True
End If
Dim blnConnected
Dim i
blnConnected = False
If enumPrinters.Exists(LCase(strPrinter)) Then
blnConnected = True
End If
'Wscript.Echo strPrinter
'Wscript.Echo blnDisconnect & " disconnect"
'Wscript.Echo blnConnected & " already connected"
'can i check if printer is already available???
If blnConnected = False Then
'msgbox strPrinter
On Error Resume Next
Err.Clear
WshNetwork.AddWindowsPrinterConnection strPrinter
If Err.Number = 0 Then
MapPrinter = 0
Else
MapPrinter = 4
'msgbox "couldnt map printer"
End If
On Error Goto 0
ElseIf blnConnected = True And blnDisconnect = "False" Then
'msgbox "cant map printer " & strPrinter & " already in use and disconnect set to false."
MapPrinter = 8
ElseIf blnConnected = True And blnDisconnect = "True" Then
'msgbox strPrinter
WshShell.RegDelete "HKCU\Printers\Connections\" & Replace(strPrinter, "\", ",") & "\"
'Wscript.Echo Err.Number & " after remove registry printer"
On Error Resume Next
'msgbox ""
WshNetwork.AddWindowsPrinterConnection strPrinter
If Err.Number = 0 Then
MapPrinter = 0
Else
MapPrinter = 2
'msgbox "couldnt map printer"
End If
On Error Goto 0
End If
End Function