Hi I am working on a vbs logon script that will add/remove network printers.
************BACKGROUND*************
We currently, use a logon script to add printers it works fine, but users where complaining that the naming convention was confusing so we changed the name of the printers. the problem arises that now they have old (invalid) printers on their computer.
My goal is to have a script that pulls a list of shared printers from the print server and maps those to client machines at logon AND it removes any invalid shared printers on the client machine (those shared non-local printers no longer being served up by the print server).
********** QUESTION **************
HOW DO I REFERENCE AND MAPPED NETWORK PRINTER THAT IS NO LONGER VALID AS A PRINTER. E.G. THE PRINTER NAME HAS CHANGED OR THE PRINTER HAS BEEN REMOVED TOTALLY.
MY CODE: MANY THANKS IN ADVANCE
Option Explicit
'On error resume next
' variables are defined here
Dim strComputer, objNetwork, objWMIService, colInstalledPrinters, objPrinter, strUNCPrinter, defaultPrinter ,strlocalComputer
Dim objlocalWMIService
' ***********************************************************
' this portion of the script will remove all shared printers
' find the printer currently defined as default and save it
' *************************************************************
' This sets the computer to all local machines
strlocalComputer = "."
Set objNetwork = CreateObject("WScript.Network")
' this is the Windows management Service object a query will be executed on
Set objlocalWMIService = GetObject("winmgmts:")
' The local mahcine is queried for all shared printers.
' The data is placed in an Array
Set colInstalledPrinters = objlocalWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")
' iterate through array deleting all returned printer objects
For Each objPrinter in colInstalledPrinters
Wscript.Echo "Name: " & objPrinter.Name & " _-_ Location: " & objPrinter.SystemName & " _-_ DeviceID =" & objPrinter.DeviceID
'Wscript.Echo "Name: " & objPrinter.DeviceID
' check for default printer status if true name value is stored for later use
If objPrinter.Default = True Then
defaultPrinter = objPrinter.Name
End If
' printer is removed
'objNetwork.RemovePrinterConnection objPrinter.DeviceId
Next
' ##################################################################################################
' This section is where the shared printers on ABOR1 are found and added to the local machine
' the default printer is defined above and used in the last step.
' ###################################################################################################
' define the machine being queried for this purposes ABOR1.
strComputer = "ABOR1"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' This query will pull all shared printer objects that have a location of ABOR
' if the location field in the printer is not = ABOR this script will not add it.
Set colInstalledPrinters = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer where Shared = True and Location = 'ABOR'")
'iterate through array adding all shared printer objects found on ABOR1
For Each objPrinter in colInstalledPrinters
'Printer name is constructed here \\server\printer
strUNCPrinter = "\\" & objPrinter.SystemName & "\" & objPrinter.ShareName
' printer is added here
'objNetwork.AddWindowsPrinterConnection strUNCPrinter
Next
'Sets the default printer after all printers have been added this comes from
if defaultPrinter <> "" then
objNetwork.SetDefaultPrinter defaultPrinter
End if


