Solved

Help with a VBS printer check and program execution

Posted on 2015-02-06
4
103 Views
Last Modified: 2015-02-20
Hi,

Can someone help us out with the following scenario:

-Our printers to are configured via GPO/Preferences
-Some users have an application that needs the printer to  mapped in order to work launch correctly. There is a little delay for the GPO to finish mapping the printer and the program sometimes starts before the default printer is mapped.  

I was wondering how hard would be to have a script that will call the app .exe as soon as the printer is available to the computer via group policy.
0
Comment
Question by:llarava
  • 2
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
Hi, you should be able to run this script at logon, which will continue to check every second, for 30 seconds, when a network printer is configured as the default printer.  If a network printer is found as the default, it will run the program defined by strExe.  If no network printer is found as the default after 30 seconds (intTimeOut) it will display a message.

Regards,

Rob.

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
strComputer = "."
' Maximum time to wait in seconds
intTimeOut = 30
' Counter start
intSeconds = 0
' Program to run when a network printer is found to be the default
strExe = "C:\Windows\Notepad.exe"
Do
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
	Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Printer WHERE Network='True' AND Default='True'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
	blnFound = False
	For Each objItem In colItems
		blnFound = True
	Next
	If blnFound = False Then
		intSeconds = intSeconds + 1
		WScript.Sleep 1000
	End If
Loop While blnFound = False And intSeconds < intTimeOut
If blnFound = True Then
	'MsgBox "Printer found."
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run strExe, 1, False
Else
	MsgBox "Timeout reached. No network printer was found as the default after " & intTimeOut & " seconds."
End If

Open in new window

0
 

Author Comment

by:llarava
Comment Utility
Rob,

Thank you very much! Would it be possible for the script to loop one time in case during the 30 seconds the printer has not been mapped? For example the script runs after 30 seconds then it runs again and tries to find the printer if it's not then will do nothing.  I understand we could extend the time to 60, however if the printer is ready sooner then we should be able to get the app launched much quicker.

Thanks!
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Hi, the script is retrying every second, up to intTimeout seconds. Within the loop, it queries the printer objects, and if a default network one is found, it exits the loop, otherwise it waits one second, and checks again.

It should work the way you want it to, but if not, I'll do more testing.

Rob,
0
 

Author Closing Comment

by:llarava
Comment Utility
Excellent! Thank you!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

762 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

10 Experts available now in Live!

Get 1:1 Help Now