Link to home
Start Free TrialLog in
Avatar of Priscilla_Hora
Priscilla_Hora

asked on

Remove printer via script

I have migrated my printers from one server (A) to another (B)
I want to remove all printers from client PCs which are attached to the old print server.
For example: \\A\printer1
How can I achieve this by using kix logon script?
Keep in mind that I only want to remove printers attached to server A, I want to leave all local printers.

Thanks
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, you could use this in a VBS file, and call it from the KIX script with
Shell 'wscript.exe yourscript.vbs'

And you can also try to convert a VBS to KIX by using this:
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=139729&site_id=1#import

Regards,

Rob.
Oh, forgot the VBS code, here it is:
'====================
strComputer = "."

strOldServer = "\\OLDPRNSVR"

Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = False")

If colPrinters.Count <> 0 Then
    For Each objPrinter In colPrinters
            If UCase(objPrinter.ServerName) = UCase(strOldServer) Then objNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
    Next
End If
'===============

Regards.

Rob.
Avatar of Priscilla_Hora
Priscilla_Hora

ASKER

That works.

How can I do the following instead:
If user is connected to \\oldserver\oldprinter then add \\newserver\newprinter and delete \\oldserver\oldprinter
with possibility of setting it to default if old printer was default.

Is that possible rather than removing the printer. Can the script see that printer 1 is connect and just change the server from old to new?
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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
Thanks for the grade.  By the way, ahoffman's link to prnport.vbs can be used, in conjuction with my script, to also remove the TCP/IP port associated with a printer.  You can strPort = objPrinter.PortName to determine the printer port, then use prnport.vbs to remove that port (after deleting the printer) with
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "cscript prnport.vbs -d -r " & strPort, 0, True

Regards,

Rob.