Link to home
Start Free TrialLog in
Avatar of trarthur
trarthur

asked on

Help with some additional logic in a vbscript

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

Next
ASKER CERTIFIED SOLUTION
Avatar of quiklearner
quiklearner

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
Avatar of quiklearner
quiklearner

The response I gave, additionally re-queries and verifies the new printer to exist and only then removes the existing one, exactly the solution that was asked for.
Avatar of trarthur

ASKER

Sorry guys...been away for awhile and forgot about it.  Thanks for the solution.