Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to open an App and wait until it it closed...HELP!

Status
Not open for further replies.

kungfusnwbrdr

Technical User
Sep 26, 2007
6
US
I'm a newb when it comes to VBScripting, so please excuse my ignorance. I'm trying to write a wrapper script for the Symantec NetBackup Administration Console to prevent two or more people from opening it at the same time. I plan to have it check for and create a lock file. What I want it to do is after the user closes the application is to delete the lock file. However, I can't seem to figure out how to make the VBScript wait while the application is open.

Can anybody help? Thank you!
 
Why not just enumerate running processes and look for the process?

Here is sample code. Start notepad on your system, then run the script.. When you close Notepad the script will let you know. Try the script again without notepad and you will see it tells you it is not running right away.

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


Do Until IsRunning = "No"
	IsRunning = CheckProcess
Loop
WScript.Echo "Notepad not running"


Function CheckProcess
	AppFound = "No"
	Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
	For Each objProcess in colProcessList
	    If objProcess.Name = "notepad.exe" Then
	    	AppFound = "Yes"
	    End If
	Next
	CheckProcess = AppFound
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
This is the way I've accomplished this...very similar to Mark's method...just providing another option here.

Code:
 WaitForProcess ".", "notepad.exe"
 WScript.Echo "Notepad is no longer running...continue with script!"

 Sub WaitForProcess(strComputer, strProcess)
     Dim wmiQuery : wmiQuery = "Select * From Win32_Process Where Name='" _
                           & strProcess & "'"
     Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & strComputer _
                                     & "\root\cimv2")
     Dim colItems : Set colItems = objWMIService.ExecQuery(wmiQuery)
 
     Dim intProcCount : intProcCount = colItems.Count
     Do While intProcCount > 0
         Set colItems = objWMIService.ExecQuery(wmiQuery)
         intProcCount = colItems.Count
         WScript.Sleep 1000
     Loop
 End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Both good methods. Also.. the .run method has built in support for waiting for a process to complete.


Set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "notepad.exe"
WaitForProcess = True

WshShell.Run strCommand, 1, WaitForProcess


You could test for the flagfile, if it doesn't exist then start the process and write the flagfile, then continue on through the script to delete the flagfile when the app exits.

I have loaded a full example to at this url:


To make use of this method you should remove the original shortcut to the app, and replace it with a shortcut to this script.

Regards.. Marty
 
Thanks all for the responses, sorry it took me a while to get back to you all, I was buried in some other work.

Marty, your solution worked great! Now I can keep multiple people out of the administration console.

Sweet...

Thanks all!

Regards,
Jason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top