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!

Checking to see if program is open/Start program if it's not open 1

Status
Not open for further replies.

skifiend

IS-IT--Management
Dec 5, 2001
18
US
Hi folks-

I'm new to vbscripting so please bear with me. The script I'm trying to write will look for a certain text in the title bar of a window. For example, My Documents. If it can't find a My Documents window open, it then will open the folder/program. What I'm working on is a thin client that I want to automatically log into a terminal server, automatically open a website, and serve as a terminal just for that website. If the terminal session gets closed, I want the thin client running a script that will automatically fire up the ternimal session again. Thanks in advance!
 
I think I answered a similar question on a different forum. If you want to monitor for a application or process, your best bet is using WMI (Win32_Process class). You could possibly monitor for the title bar title using wscript.shell, but it may not be as reliable. Here is a simple example of looking for an application using WMI. Run it once with notepad running and once without it. You will simply need to modify it so it queries on an interval and start your application. You could also use __instancedeletionevent with WMI. Put something together and if you run into any errors post it.

example:
Option Explicit
'On Error Resume Next

Dim strComputer, wmiNS, wmiQuery, objWMIService
Dim colItems, objItem

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * From win32_process Where name='notepad.exe'"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

If colItems.Count = 0 Then
WScript.Echo "start my app again"
Else
WScript.Echo "my app is running"
End If

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
dm4ever-

Many thanks! I've modified it slightly so that instead of popping up dialog boxes indicating your program state, I have it fire up my Remote Desktop Connection if it's not running. I want to be able to go right into an existing profile setup, and *.rdp file. Can I do that? Thanks again!
 
I forgot to add my script...

Dim strComputer, wmiNS, wmiQuery, objWMIService
Dim colItems, objItem

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * From win32_process Where name='mstsc.exe'"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

If colItems.Count = 0 Then
Const SW_NORMAL = 1
strComputer = "."
strCommand = "mstsc.exe"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = SW_NORMAL
Set objProcess = objWMIService.Get("Win32_Process")
intReturn = objProcess.Create _
(strCommand, Null, objConfig, intProcessID)
Else

End If
 
You should be able to use your existing profile by simply modifying your strCommad to be something like.

strCommand = "mstsc.exe c:\temp\defaultprofile.rdp"

Also note that the current query will run once and not monitor for it unless you change the logic a little to keep the script running and checking periodically.

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
Here's my script:

DIM intCount
intCount = True

DO while intCount = True

Dim strComputer, wmiNS, wmiQuery, objWMIService
Dim colItems, objItem

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * From win32_process Where name='mstsc.exe'"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

If colItems.Count = 0 Then
'Const SW_NORMAL = 1
strComputer = "."
strCommand = "mstsc.exe c:\remotedesktopconnections\default.rdp"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = SW_NORMAL
Set objProcess = objWMIService.Get("Win32_Process")
intReturn = objProcess.Create _
(strCommand, Null, objConfig, intProcessID)
Else
WScript.Sleep 3000
End If
Loop

Everything seems to work great on my laptop I created and tested this script on. My ulimate goal is to put this in the Startup folder on a HP Thin Client. When I try to fire up the script on the Thin Client, I get the following error: There is no file extension in "C:\Documents"

I'm beyond confused now! Any more help is greatly appreciated.
 
Is that all the error says? Does it provide a line number?

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
That is all it says when the script is run from the Startup dir. in the start menu. If I move the script to the root C drive and run it, I get an invalid syntax error. Again, this script ran perfectly on my laptop, and starting it from multiple directory locations. It only screws up on the thin client.
 
What if you tried something like this.

Dim strComputer, wmiNS, wmiQuery, objWMIService
Dim colItems, objItem, objShell

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * From win32_process Where name='mstsc.exe'"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set objShell = CreateObject("WScript.Shell")

Do
Set colItems = objWMIService.ExecQuery(wmiQuery)

If colItems.Count = 0 Then
objShell.Run "mstsc ""C:\Documents and Settings\Jose.Martinez2\Desktop\2ndary_Box.rdp"""
Else
WScript.Sleep 3000
End If
Loop

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
In your case it would be:

objShell.Run "mstsc.exe c:\remotedesktopconnections\default.rdp"

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
I just tried your script and when it's run from the startup directory, I still get the weird "There is no file extension in "C:\Documents" error. When I run your script from the root of C on the thin client, I get an invalid syntax error with Line 7, Char 1. Again, I ran your script on my laptop first and worked like a charm! Thanks for all your help!
 
Line 7 in your script and line 13 in my script are the same and that's where it seems to be screwing up.
 
What OS is that thin client running? Are you able to run wbemtest? Seems the error is when trying to connect to WMI.


start > run > wbemtest
press "connect" > change "root\default" to "root\cimv2" > press "open class" > type win32_process > press enter > press the "instances" button > there should be a list of processes here.

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
Does the login you use on the thin client have admin rights to the box?

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
The OS on the thin client is XP Embedded. I tried to run the wbemtest and windows can't find it. I have tried running the script while logged in as administrator on the thin client as well and nothing happens. Maybe a vbscript won't work on a thin client?
 
I'm not too familiar with XP Embedded, but I saw that WMI is a component you can add with XP Embedded SP2. This is a workaround if you can't get WMI to work. Hope it helps and/or works for you.

Option Explicit
'On Error Resume Next

Dim objShell

Set objShell = CreateObject("WScript.Shell")

Do
If (objShell.AppActivate("Remote Desktop") = False) Then
objShell.Run "mstsc.exe c:\remotedesktopconnections\default.rdp"
WScript.Sleep 5000
Else
WScript.Sleep 3000
End If
Loop


dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
That last script works great! I really, really appreciate it! I owe you an e-Beer
 
I am going through the same issue. I am using an HP thin client running XPe and I cannot make use of WMI. It's driving me crazy!

If either of you could shed some light on terminating a process without using WMI I would very much appreciate it!
 
I would suggest looking at AutoIt. It has a function called ProcessClose that can do this. AutoIt will also let you package it into an exe so that you don't even need AutoIt installed on the machine you run it on.


--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top