Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Printer Script not working

Status
Not open for further replies.

dlingo

Programmer
Nov 19, 2001
60
US
I have written a script in an attempt to remove all printers for the current user logged onto a windows workstation and I am getting a "Network connection does not exist" error. However, the connection is there. The code is as follows:

On Error Resume Next
Dim wshNetwork, oPrinters, Count
Set wshNetwork = CreateObject("WScript.Network")
Set oPrinters = wshNetwork.EnumPrinterConnections
Count = oPrinters.Count

Do While Count <> 0
wshNetwork.RemovePrinterConnection oPrinters.Item(Count-1), True, True
If Err.Number <> 0 Then
WScript.Echo Err.Description
Else
WScript.Echo oPrinters.Item(Count-1) & &quot; Removed&quot;
End If
Count = Count -1
Loop

oPrinters.Item(Count-1) returns the following:
even numbers - &quot;FPR4:&quot; (port)
odd numbers - &quot;\\ServerName\PrinterName&quot; (printer)
-all returned values are valid

Error: Network Connection Does Not Exist

How can a network connection not exist when it was just found using the wshNetwork.EnumPrinterConnections(Item) property?
 
is it not just your Do Loop and count that has a problem???
what happens when you pipe the string that you are trying to remove??? i.e. check with an echo what is oPrinters.Item(Count-1)

i have given up with EnumPrinters collections. It causes issues when printers\printer servers arent available. I use the registry to determine what printers are connected and delete them via the registry as well.

good luck
 
mrmovie,

Thanks for your response. For debug purposes I used a WScript.Echo oPrinters.Item(Count-1) and all was correct. I've pretty much decided to follow the registry root as well. However, once I remove the printers from the registry, I need to make windows aware of this immediately, so that the user doesn't have to log off and back on before these changes take affect. Then I will be adding specific printers (much easier than removing).

Can this be done in scripting?

I've been making attempts with the Windows API, which I'm rather new to. I've been using a broadcast system message within VB6, but haven't quite got it yet.
 
i do it via the registry and havent had problems with it not updating, perhaps i am doing something wrong. that was during the testing.

i implement it at logon, perhaps that makes a difference.
this what i have to delete a printer and\or reconnect that old printer to a new print server...and then a script to map printers if its of help. mine only work for network printers though, had a request to support local printers but i dont really want to go there

Private Sub swapPrintServer(str2Swap)
On Error Resume Next
Dim sPSArray

If blnRegPrinter = False Then
'msgbox &quot;init printers&quot;
Dim strSPRootKey, arrSPSubKeys, refSPRegistry, subSPKey, strSPPrinterMapping

Set enumSPPrinters = WScript.CreateObject(&quot;Scripting.Dictionary&quot;)
Set refSPRegistry = GetObject(&quot;winmgmts:root\default:StdRegProv&quot;)
Const HKEY_CURRENT_USER = &H80000001
strSPRootKey = &quot;Printers\Connections&quot;

If refSPRegistry.EnumKey(HKEY_CURRENT_USER, strSPRootKey, arrSPSubKeys) = 0 Then
'init dictionary object of printers
For Each subSPKey In arrSPSubKeys
strSPPrinterMapping = &quot;&quot;
strSPPrinterMapping = Trim(LCase(CStr(subSPKey)))
If InStr(strSPPrinterMapping, &quot;,,&quot;) = 1 Then
strSPPrinterMapping = Replace(strSPPrinterMapping, &quot;,&quot;, &quot;\&quot;)
If enumSPPrinters.Exists(strSPPrinterMapping) Then
'already have this one, unlikely
Else
'Wscript.Echo &quot;adding &quot; & strPrinterMapping & &quot; to enumPrinters&quot;
enumSPPrinters.Add strSPPrinterMapping, &quot;1&quot;
End If
End If
Next
End If
Set refSPRegistry = Nothing
blnRegPrinter = True
End If

Call LOG_BUFFER(&quot;SWAP PRINTER SUB, TRYING TO SWAP/DISCONNECT &quot; & str2Swap, &quot;full&quot;, &quot;full&quot;)

'aCGC = &quot;\\braps01a&\\172.25.12.254&quot;
sPSArray = Split(str2Swap, &quot;&&quot;, -1, 1)

If UBound(sPSArray) = 0 Then
'msgbox &quot;only dlete not readd&quot;
'just want to delete old printers

If enumSPPrinters.Exists(LCase(CStr(sPSArray(0)))) Then
Call LOG_BUFFER(&quot;ONLY GOING TO REMOVE PRINTER CALLED &quot; & LCase(CStr(sPSArray(0))), &quot;FULL&quot;, &quot;FULL&quot;)
WshShell.RegDelete &quot;HKCU\Printers\Connections\&quot; & Replace(LCase(CStr(sPSArray(0))), &quot;\&quot;, &quot;,&quot;) & &quot;\&quot;
Call LOG_BUFFER(&quot;Err Number = &quot; & Err.Number, &quot;FULL&quot;, &quot;FULL&quot;)
Call LOG_BUFFER(&quot;AFTER REMOVE PRINTER CALL &quot; & LCase(CStr(sPSArray(0))), &quot;FULL&quot;, &quot;FULL&quot;)
End If

End If

If UBound(sPSArray) = 1 Then
'msgbox &quot;delete and re add&quot;
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(&quot;GOING TO REMOVE THEN RE-MAP TO NEW SERVER&quot;, &quot;FULL&quot;, &quot;FULL&quot;)
Call LOG_BUFFER(&quot;GOING TO REMOVE &quot; & enumSPPrinters.Item(i), &quot;FULL&quot;, &quot;FULL&quot;)
WshShell.RegDelete &quot;HKCU\Printers\Connections\&quot; & Replace(LCase(CStr(enumSPPrinters.Item(i))), &quot;\&quot;, &quot;,&quot;) & &quot;\&quot;
Call LOG_BUFFER(&quot;AFTER REMOVE PRINTER CALL &quot; & enumSPPrinters.Item(i), &quot;FULL&quot;, &quot;FULL&quot;)
Call LOG_BUFFER(&quot;BEFORE RE-MAP PRINTER CALL TO SERVER &quot; & sPSArray(1), &quot;FULL&quot;, &quot;FULL&quot;)
waspMach.MapPrinter sPSArray(1) & &quot;\&quot; & Right(LCase(CStr(enumSPPrinters.Item(i))), Len(CStr(enumSPPrinters.Item(i))) - Len(sPSArray(0)) - 1), &quot;False&quot;
Call LOG_BUFFER(&quot;AFTER RE-MAP PRINTER CALL TO SERVER &quot; & sPSArray(1), &quot;FULL&quot;, &quot;FULL&quot;)
End If
Next
End If

End Sub



Public Function MapPrinter(strPrinter, blnDisconnect)
'initilize printers dicObject
On Error Resume Next
If blnMapPrinter = False Then
'msgbox &quot;init printers&quot;
Dim strRootKey, arrSubKeys, refRegistry, subKey, strPrinterMapping

Set enumPrinters = WScript.CreateObject(&quot;Scripting.Dictionary&quot;)
Set refRegistry = GetObject(&quot;winmgmts:root\default:StdRegProv&quot;)
Const HKEY_CURRENT_USER = &H80000001
strRootKey = &quot;Printers\Connections&quot;

If refRegistry.EnumKey(HKEY_CURRENT_USER, strRootKey, arrSubKeys) = 0 Then
'init dictionary object of printers
For Each subKey In arrSubKeys
strPrinterMapping = &quot;&quot;
strPrinterMapping = Trim(LCase(CStr(subKey)))
If InStr(strPrinterMapping, &quot;,,&quot;) = 1 Then
strPrinterMapping = Replace(strPrinterMapping, &quot;,&quot;, &quot;\&quot;)
If enumPrinters.Exists(strPrinterMapping) Then
'already have this one, unlikely
Else
'Wscript.Echo &quot;adding &quot; & strPrinterMapping & &quot; to enumPrinters&quot;
enumPrinters.Add strPrinterMapping, &quot;1&quot;
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 & &quot; disconnect&quot;
'Wscript.Echo blnConnected & &quot; already connected&quot;
'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 &quot;couldnt map printer&quot;
End If
On Error Goto 0
ElseIf blnConnected = True And blnDisconnect = &quot;False&quot; Then
'msgbox &quot;cant map printer &quot; & strPrinter & &quot; already in use and disconnect set to false.&quot;
MapPrinter = 8
ElseIf blnConnected = True And blnDisconnect = &quot;True&quot; Then
'msgbox strPrinter

WshShell.RegDelete &quot;HKCU\Printers\Connections\&quot; & Replace(strPrinter, &quot;\&quot;, &quot;,&quot;) & &quot;\&quot;
'Wscript.Echo Err.Number & &quot; after remove registry printer&quot;
On Error Resume Next
msgbox &quot;&quot;
WshNetwork.AddWindowsPrinterConnection strPrinter
If Err.Number = 0 Then
MapPrinter = 0
Else
MapPrinter = 2
'msgbox &quot;couldnt map printer&quot;
End If
On Error Goto 0
End If

End Function
 
glad i posted that as i have just noticed a bug!!! didnt want the Msgbox &quot;&quot; to be there in the live environment
 
And this ?
Code:
Do While Count
>
Code:
 0
  wshNetwork.RemovePrinterConnection oPrinters.Item(Count-1), True, True
  If Err.Number <> 0 Then
    WScript.Echo Err.Description
  Else
    WScript.Echo oPrinters.Item(Count-1) & &quot; Removed&quot;
  End If
  Count = Count -
2
Code:
Loop

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top