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

Silvan
Silvan used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
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
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 Engineer

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial