Solved

Help with a VBS printer check and program execution

Posted on 2015-02-06
4
110 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

An article on effective troubleshooting
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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