Check how many printers are mapped and combine it with an if condition

Dear community,
Actually we plan to optimize our VBS logon script (unfortunately my VBS skills are not the best).
There is a row, in which we start an external application to set the customer's default printer (setdefaultprinter.exe). It is a simple program, which lets the user choose his favourite printer.
Some users do not have a printer or only one. The program "setdefaultprinter.exe" should only start if the users has two or more printer mapped.

Something like that:
If "2 or more printers are available" run setdefaultprinter.exe
else do nothing.

Based on some internet research I found the following script:

Function printerExists(str)
    printerExists = False
    Dim objWMIService
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 
    Dim colPrinters
    Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
 
    Dim objPrinter
    For Each objPrinter In colPrinters
        If objPrinter.Name = str Then
            printerExists = True
            Exit For
        End If
    Next
End Function

But how can I combine it with my "IF condition"?

Thanks for any help

Best regards,
steffeninf
SilvanSystem EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Function printerCount()
     printerCount = 0
     Dim objWMIService
     Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  
     Dim colPrinters
     Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
  
     printerCount = colPrinters.Count
End Function

Dim oShell
Set oShell = WScript.CreateObject("WScript.Shell")
If printerCount >= 2 Then
    oShell.Run "setdefaultprinter.exe"
Else
    ' Another thing
End If
Set oShell = Nothing

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike DuckettCommented:
That script will check whether a particular printer exists (you pass the name when calling the function).  If you were just trying to count I would change it to (not tested):

Function printerCount()
     Dim objWMIService
     Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 
     Dim colPrinters
     Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
 
     Dim objPrinter
     For Each objPrinter In colPrinters
         Count = Count + 1
     Next
If Count > 2 then
Run your external app here
End If
End Function

Count variable then holds the number of printers installed (this will include things like the XPS writer/pdf writer etc if office is installed)
SilvanSystem EngineerAuthor Commented:
Thanks for your assistance. I was able to add the following code to the logon script:

Set objShell	= CreateObject("Wscript.Shell")
' --- Define Default Printer ---------------------------
                If printerCount >= 2 Then
                               objShell.Run strLogonServer+".%FQDN%\NETLOGON\SetDefaultPrinter\SetDefaultPrinter.exe /set"
                Else
                               'Do Nothing
                End If


'*****************************************************************************************************************
'* Function:             PrinterCount()
'* Count all printerqueues
'*****************************************************************************************************************
Function printerCount()
     printerCount = 0
     Dim objWMIService
     Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  
     Dim colPrinters
     Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
  
     printerCount = colPrinters.Count
End Function

Set objShell = Nothing

Open in new window


Best regards,
steffeninf
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.