Solved

Help with a VBS printer check and program execution

Posted on 2015-02-06
4
113 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 40594791
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
ID: 40598940
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
ID: 40599258
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
ID: 40622028
Excellent! Thank you!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

724 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