Link to home
Start Free TrialLog in
Avatar of mhdcommunications
mhdcommunications

asked on

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.
Avatar of Slav Zabicki
Slav Zabicki
Flag of Canada image

do u have the printers listed in AD  DS?
 What version of 2003 do u have ? r2?
Avatar of mhdcommunications
mhdcommunications

ASKER

The printers are not listed in AD, and its Server 2003 SP2, not R2
ASKER CERTIFIED SOLUTION
Avatar of Slav Zabicki
Slav Zabicki
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Both solutions worked, the one from zabicki is what i ended up going with.