Link to home
Start Free TrialLog in
Avatar of Silvan
Silvan

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

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
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)
Avatar of Silvan
Silvan

ASKER

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