Adding a new printer share then remove old



The situation I'm in is we are renaming all our printers and shares. I have
used in the past a script that checks for the old printer, installs the new
one, checks for if it was a default then deletes the old. Unfortunately
this was when we where changing server names and the difference is we are
keeping the same server name but changing the printer names and shares.
However I can't seem to get this script working because the old printer name
no longer exists because it has been renamed. Is there a way around this?
Below I have a copy of our test script

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")


For Each objPrinter in colPrinters

on error resume next

If Lcase(objPrinter.name) = Lcase("\\appprint\test1") Then
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\appprint\testprint1"


Else


If Lcase(objPrinter.name) = Lcase("\\appprint\test2") Then
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\appprint\testprint2"


End If

End If

Next

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where
Default = TRUE")


For Each objPrinter in colPrinters

If Lcase(objPrinter.name) = Lcase("\\appprint\test1") Then
Set objNetwork = CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter "\\appprint\testprint1"


Else

If Lcase(objPrinter.name) = Lcase("\\appprint\test2") Then
Set objNetwork = CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter "\\appprint\testprint2"



End If

End If

Next

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

For Each objPrinter in colPrinters

on error resume next

If Lcase(objPrinter.name) = Lcase("\\appprint\testprint1") Then
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.RemovePrinterConnection "\\\appprint\test1"

Else

If Lcase(objPrinter.name) = Lcase("\\appprint\testprint2") Then
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.RemovePrinterConnection "\\appprint\test2"



End If

End If

Next


.



Relevant Pages

  • Adding a new printer share then remove old
    ... The situation I'm in is we are renaming all our printers and shares. ... However I can't seem to get this script working because the old printer name ... For Each objPrinter in colPrinters ... Set objNetwork = CreateObject ...
    (microsoft.public.scripting.wsh)
  • Re: Error 8007007E WScript.Network Wscript.CreateObject
    ... Dim objNetwork ... Set objNetwork = WScript.CreateObject ... company application, the script runs. ... This problem frequently occurs after you upgrade a service pack. ...
    (microsoft.public.scripting.vbscript)
  • Re: Logon scripts dont run on 64-bit machines?
    ... Copyright 1985-2003 Microsoft Corp. ... Microsoft Windows Script Host Version 5.6 ... Set ObjNetwork = CreateObject ... Dim strDC, strOU, strOU1, strOU2, intIndex, intIdx ...
    (microsoft.public.scripting.wsh)
  • Re: Error 8007007E WScript.Network Wscript.CreateObject
    ... Dim objNetwork ... Set objNetwork = WScript.CreateObject ... company application, the script runs. ... This problem frequently occurs after you upgrade a service pack. ...
    (microsoft.public.scripting.vbscript)
  • How to detect mapped drives with WSH
    ... script so we can add it as a policy. ... as long as the mapped drives are not present. ... Set objNetwork = Wscript.CreateObject ...
    (microsoft.public.scripting.wsh)