Solved

Help with a VBS printer check and program execution

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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