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

Next
ASKER CERTIFIED SOLUTION
quiklearner

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 3 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 3 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004