• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 883
  • Last Modified:

How can you remove network printers mapped by a vbscript from a terminal server/workstation?

I have a vbscript that used to map printers to all users in my domain. I have since moved away from this and am now using a third party app to manage printers. I need to be able to remove all the printers that were mapped using the script. I have tried a few different codes i found online (below) but they either do nothing at all (no errors, printers stay) or it errors out and says 'The network path cannot be found'. The print server these were mapped from is no longer in service, so these connections are no longer valid.
This is a sample of my code
Option Explicit 

 Sub AssignNetworkPrinters(default)

  Dim multiPrinter
  Dim objNetwork, strUser, objUser, group

  Set objNetwork = CreateObject("WScript.Network")
  strUser = objNetwork.UserName
  Set objUser = GetObject("WinNT://printserver/" & strUser)  
  Set multiPrinter = CreateObject("WScript.Network")
  multiPrinter.AddWindowsPrinterConnection "\\printserver\2055"
  multiPrinter.AddWindowsPrinterConnection "\\printserver\HPOff"
  multiPrinter.AddWindowsPrinterConnection "\\printserver\HPP1"
  multiPrinter.AddWindowsPrinterConnection "\\printserver\HPP16"
  multiPrinter.AddWindowsPrinterConnection "\\printserver\Xero"
 End Sub 'AssignNetworkPrinters()

 Sub Main()

  Dim default
  default = "."

  Call AssignNetworkPrinters(default)

 End Sub 'Main()


Call Main()

WScript.Quit

'End Script

Open in new window


I have tried using RemovePrinterConnection, and get 'The Network Path cannot be found'
 
Option Explicit 

 Sub AssignNetworkPrinters(default)

  Dim multiPrinter
  Dim objNetwork, strUser, objUser, group

  Set objNetwork = CreateObject("WScript.Network")
  strUser = objNetwork.UserName
  Set objUser = GetObject("WinNT://printserver/" & strUser)  
  Set multiPrinter = CreateObject("WScript.Network")
  multiPrinter.RemovePrinterConnection "\\printserver\2055"
  multiPrinter.RemovePrinterConnection "\\printserver\HPOff"
  multiPrinter.RemovePrinterConnection "\\printserver\HPP1"
  multiPrinter.RemovePrinterConnection "\\printserver\HPP16"
  multiPrinter.RemovePrinterConnection "\\printserver\Xero"
 End Sub 'AssignNetworkPrinters()

 Sub Main()
`
  Dim default
  default = "."

  Call AssignNetworkPrinters(default)

 End Sub 'Main()


Call Main()

WScript.Quit

'End Script

Open in new window


This code is supposed to remove all networked printers, but does absolutely nothing.
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set Printers = WshNetwork.EnumPrinterConnections

For i = 0 to Printers.Count - 1 Step 2

    If Left(ucase(Printers.Item(i+1)),2) = "\\" Then
        WScript.Echo Printers.Item(i+1)
        WSHNetwork.RemovePrinterConnection Printers.Item(i+1)
    End IF
Next

Open in new window



I also tried this code, and it does nothing as well.
 
strComputer = "printserver"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Network = TRUE")

For Each objPrinter in colInstalledPrinters
    objPrinter.Delete_
Next

Open in new window



There are 6 Windows Terminal Servers that are affected, and over 100 PCs affected. Manually removing the printers from each PC would be a nightmare. If there is a way to remove ALL printers from the machine, i may give that a try - or remove ALL printers mapped from a specific server. Understand the mapped printers no longer exist, which may be why the script says 'The network path can not be found', but there is no way to bring the old print server back online with the shared printers - it's gone.

Thanks in advance.
0
mhdcommunications
Asked:
mhdcommunications
  • 3
  • 2
2 Solutions
 
Slav ZabickiSystem EngineerCommented:
do u have the printers listed in AD  DS?
 What version of 2003 do u have ? r2?
0
 
mhdcommunicationsAuthor Commented:
The printers are not listed in AD, and its Server 2003 SP2, not R2
0
 
Slav ZabickiSystem EngineerCommented:
create a logon script for the users or computers
try

Option Explicit

Dim objNetwork, objPrinters
Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections


Dim sPrinterName
sPrinterName = "\\printserver\2055"
objNetwork.RemovePrinterConnection sPrinterName, True, True

sPrinterName = "\\printserver\HPOff"
objNetwork.RemovePrinterConnection sPrinterName, True, True

sPrinterName = "\\printserver\HPP1"
objNetwork.RemovePrinterConnection sPrinterName, True, True

sPrinterName = "\\printserver\HPP16"
objNetwork.RemovePrinterConnection sPrinterName, True, True

sPrinterName = "\\printserver\Xero"
objNetwork.RemovePrinterConnection sPrinterName, True, True
 
0
 
Slav ZabickiSystem EngineerCommented:
or a hardcore version

create a batch

reg delete "hkcu\printers\connections" /f

deploy in AD
0
 
mhdcommunicationsAuthor Commented:
Both solutions worked, the one from zabicki is what i ended up going with.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now