Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with a VBS printer check and program execution

Posted on 2015-02-06
4
Medium Priority
?
118 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

879 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