Solved

How to use vb script to check if a network shared printer is connected

Posted on 2008-10-14
17
858 Views
Last Modified: 2012-05-05
The duration of logon time is terrible so I am modifying the Logon script to check if the printer is already mapped before connecting again to cut down on time needed to login. I just cant figure out how to get it to check if the connection exists...Ive been looking around on the net, to no avail...any ideas? I am hoping for a simple if then statement.
0
Comment
Question by:knada242
  • 9
  • 7
17 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 22714508
Here is a way to use WMI to check whether the printer is mapped.


strPrinter = "\\server\share"
 

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

For Each objPrinter in colPrinters

    If Not objPrinter.Attributes And 64 Then 

        If LCase(StrPrinter) = LCase(objPrinter.ServerName & "\" & objPrinter.ShareName) Then

            blnConnected = True

        End If

    End If

Next
 

If blnConnected Then

    WScript.Echo strPrinter & " is connected."

Else

    WScript.Echo strPrinter & " is not connected."

End If

Open in new window

0
 
LVL 76

Expert Comment

by:David Lee
ID: 22714522
Hi, knada242.

Here's one approach.  Useage would be

    If CheckPrinter(".","SomePrinterName") Then

This uses WMI to query the local machine for a shared network printer with a given name.
Function CheckPrinter(strComputer, strPrinter)

	On Error Resume Next

	Const wbemFlagReturnImmediately = &h10

	Const wbemFlagForwardOnly = &h20

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

    Set colItems = objWMIService.ExecQuery("SELECT ShareName FROM Win32_Printer WHERE ShareName='" & strPrinter & "'", "WQL", wbemFlagReturnImmediately)

    If colItems.count > 0 Then

        CheckPrinter = True

    Else

        CheckPrinter = False

    End If

    Set colItems = Nothing

    Set objWMIService = Nothing

End Function

Open in new window

0
 
LVL 1

Author Comment

by:knada242
ID: 22714600
bear in mind that I am checking for multiple printers, in some cases as much as 8. so I would rather not have the printer set as a variable.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22714786
Sorry, I don't understand.  Don't you want something like

If Not Printer1 then install Printer1
If Not Printer2 then install Printer2

I can change the code to use an array of printers, but functionally that's no different than passing the printer name to check as a variable.
0
 
LVL 1

Author Comment

by:knada242
ID: 22714966
yes thats exactly what I am looking to do.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22715045
This version uses an array of printer names and a FOR ... NEXT loop.
On Error Resume Next

Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20

strComputer = "."

'Edit the list of printer names on the following line'

arrPrinters = Array("Printer1","Printer2","Printer3")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

For Each strPrinter in arrPrinters

    Set colItems = objWMIService.ExecQuery("SELECT ShareName FROM Win32_Printer WHERE ShareName='" & strPrinter & "'", "WQL", wbemFlagReturnImmediately)

    If colItems.count > 0 Then

        'The printer already exists'

    Else

        'Install the printer'

    End If

Next

Set colItems = Nothing

Set objWMIService = Nothing

Open in new window

0
 
LVL 1

Author Comment

by:knada242
ID: 22715054
wait, so I could pass it as strPrinter = (printer1,printer2,printer3)

and plug in strPrinter as the variable and it will run that code with each one of the printers as a seperate line of code?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22715064
Yes, that's what the original version did.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 76

Expert Comment

by:David Lee
ID: 22715093
Sorry, hit Enter too quick.  The original version didn't take an array of printers, it took each check as a separate call to the function.  Something like

If CheckPrinter(".","Printer1") Then InstallPrinter1
If CheckPrinter(".","Printer2") Then InstallPrinter2

Where InstallPrinter1 or 2 is a call to your code for installing the printer.
0
 
LVL 1

Author Comment

by:knada242
ID: 22715235
and that will map for all users unless other wise defined right?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22715280
Assuming that all users have/need to have the same printers, yes.  You could add to the code to make separate checks for different people or groups of people.
0
 
LVL 1

Author Comment

by:knada242
ID: 22715433
well this is what I have now, this is the most extreme case.

For Each GroupObj In UserObj.Groups

    Select Case GroupObj.Name

	 Case "CorpIT"

	      oNet.AddWindowsPrinterConnection "\\Avrdata03\Service And Rentals"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\Rentals Warehouse"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\Warehouse"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\Brady Label"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\Project Manager Area"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\ICG Plotter"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\HP 11x17"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\Sales Main Area"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\Multipurpose Room"

		  oNet.AddWindowsPrinterConnection "\\Avrdata03\Sales Front Office"

    End Select

Open in new window

0
 
LVL 1

Author Comment

by:knada242
ID: 22715497
there is a section like I noted for each Group most users use their own pc all the time but every once and a while they may need to use another pc and I want to make sure that their printers will be mapped, also there are pcs in community areas conference rooms etc etc
0
 
LVL 76

Expert Comment

by:David Lee
ID: 22716597
If you use the code from my first post, then something like this should work.
For Each GroupObj In UserObj.Groups

    Select Case GroupObj.Name

        Case "CorpIT"

            If Not CheckPrinter(".", "Service And Rentals") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\Service And Rentals"

            If Not CheckPrinter(".", "Rentals Warehouse") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\Rentals Warehouse"

            If Not CheckPritner(".", "Warehouse") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\Warehouse"

            If Not CheckPrinter(".", "Brady Label") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\Brady Label"

            If Not CheckPrinter(".", "Project Manager Area") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\Project Manager Area"

            If Not CheckPrinter(".", "ICG Plotter") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\ICG Plotter"

            If Not CheckPrinter(".", "HP 11X17") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\HP 11x17"

            If Not CheckPrinter(".", "Sales Main Area") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\Sales Main Area"

            If Not CheckPrinter(".", "Multipurpose Room") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\Multipurpose Room"

            If Not CheckPrinter(".", "Sales Front Office") Then oNet.AddWindowsPrinterConnection "\\Avrdata03\Sales Front Office"

    End Select

Open in new window

0
 
LVL 76

Expert Comment

by:David Lee
ID: 22716616
Or, if you prefer the code from my second post, then you can use something like this.
On Error Resume Next

Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20

strComputer = "."

'Edit the list of printer names on the following line'

arrPrinters = Array("Service And Rentals","Rentals Warehouse","Warehouse")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

For Each strPrinter in arrPrinters

    Set colItems = objWMIService.ExecQuery("SELECT ShareName FROM Win32_Printer WHERE ShareName='" & strPrinter & "'", "WQL", wbemFlagReturnImmediately)

    If colItems.count > 0 Then

        'The printer already exists'

    Else

        oNet.AddWindowsPrinterConnection "\\Avrdata03\" & strPrinter

    End If

Next

Set colItems = Nothing

Set objWMIService = Nothing

Open in new window

0
 
LVL 1

Author Comment

by:knada242
ID: 22723602
thanks a TON it works perfect...I know I am trying to get a lot for 500pts but how would I change this line to look for mapped drives
    Set colItems = objWMIService.ExecQuery("SELECT ShareName FROM Win32_Printer WHERE ShareName='" & strPrinter & "'", "WQL", wbemFlagReturnImmediately)

Open in new window

0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 22724137
Cool.  No problem on the additional question.  If you want all drives, then this will work:

   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_MappedLogicalDisk", "WQL", wbemFlagReturnImmediately)

If you want a specific drive, then something like this:

   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_MappedLogicalDisk WHERE Name='" & strDriveLetter & "'", "WQL", wbemFlagReturnImmediately)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now