troubleshooting Question

Help with some additional logic in a vbscript

Avatar of trarthur
trarthur asked on
VB Script
3 Comments1 Solution308 ViewsLast Modified:
Thanks to the experts here I have a script that will delete a network printer and replace it with another on a different server.

In my testing I discovered that some of my permissions are not same from one printer to the other (my problem).
Right now the script will see the old printer, delete it, and then fail to install the replacement.  Which leaves the user without a printer.  Bad news for me.

What I'd like to be able to do is add some additional logic in the script that will confirm the addition of the replacement printer prior to deleting the old one.  Here is the script with one printer:

Option Explicit
Dim objWMIService, objPrinter, colItems, strComputer, objWshShell, strDefaultState

strComputer ="."

Set objWshShell = WScript.CreateObject("WScript.Shell")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
' For Remote:
' Set Locator = CreateObject("WbemScripting.SWbemLocator")
' Set objWMIService = Locator.ConnectServer(strComputer, "root\cimv2",
' strUserName, strPassword)

Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")

' On Error Resume Next
' ***Share name:  printer1***
For Each objPrinter In colItems
If UCase(objPrinter.DeviceID) = UCase("\\server1\printer1") Then
   'Wscript.Echo "Found match " & objPrinter.DeviceID & ", replacing"
   ' Add new printer. Need the ,1,true to wait for shell to complete before continue
   objWshShell.Run "rundll32 printui.dll,PrintUIEntry /in /Gw /q /n ""\\server2\printer2"" ",1, True
   ' Remove the old
   objWshShell.Run "rundll32 printui.dll,PrintUIEntry /dn /q /n ""\\server1\printer1"" ",1, True
If objPrinter.Default Then ' if it's the default set as default
     objWshShell.Run "rundll32 printui.dll,PrintUIEntry /y /n ""\\server2\printer2"" ",1, True
  End If

End If

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros