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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do Loop needs some work 1

Status
Not open for further replies.

smurfhell

MIS
Nov 5, 2004
45
US
This my current script I have to logoff a user on a terminal as soon as they exit a certain program.
Code:
Set WshShell = WScript.CreateObject ("WScript.Shell")
strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
WScript.Sleep(15000)
Do
Set colProcessList = objWMI.ExecQuery _
("Select * from Win32_Process Where Name = 'app.exe'")
If colProcessList.Count = 0 Then
wshShell.Run "c:\windows\system32\logoff.exe"
End If
Loop
This works just fine because the computer ends all processes and logs off. However, I now want to run another process when a certain process ends. Because of the Do Loop, infinite processes are started...i.e.
Code:
Set WshShell = WScript.CreateObject ("WScript.Shell")
strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
WScript.Sleep(15000)
Do
Set colProcessList = objWMI.ExecQuery _
("Select * from Win32_Process Where Name = 'app.exe'")
If colProcessList.Count = 0 Then
wshShell.Run "c:\windows\system32\calc.exe"
End If
Loop
spawns "infinite" instances of Calculator. I'm not good with Do, Loop, and Until, so any help would be appreciated.
 
What if you added a variable to be used as a flag for when you already did it once?

Code:
[highlight]AlreadyDidIt = False[/highlight]
Do
  Set colProcessList = objWMI.ExecQuery _
    ("Select * from Win32_Process Where Name = 'app.exe'")
  If [highlight]([/highlight](colProcessList.Count = 0) [highlight]And (AlreadyDidIt = False))[/highlight] Then
    wshShell.Run "c:\windows\system32\calc.exe"
    [highlight]AlreadyDidIt = True[/highlight]
  End If
Loop




Or another idea is just to exit the loop and run the calc after the loop is done:
Code:
Do
  Set colProcessList = objWMI.ExecQuery _
    ("Select * from Win32_Process Where Name = 'app.exe'")
  If colProcessList.Count = 0 Then
    [highlight]Exit Do [/highlight]   
  End If
Loop
wshShell.Run "c:\windows\system32\calc.exe"
.... That might be best because it also gets rid of your infinate loop... before you were relying on the shutdown to end the infinite loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top