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

Check to see if a program is running and restart it

Status
Not open for further replies.

amunn

IS-IT--Management
Jul 30, 2008
12
US
Hello,

I am trying to design a program that will check to see if a program is running and how many times it is running and completely kill it until it is gone. I then want the program to run a defrag and open one of the programs back up passing their user name and password as a variable through the script so that it will be completely logged back on as it was before. I started out programming this as a batch file but I could not get the checking part of it to work, so now I am to programming in vbs but having difficulty there too, does anyone possibly have any ideas or sample scripts that I might be able to look at and see what I am doing wrong?

Thanks,
Andrew Munn
 
see what I am doing wrong?
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I've got the part with the if statement where it sees if the object is running but I can't get the part where it will work in cmd through my vbs script. The rest of our script needs to be executed through the command line - this is the start of my script, instead of doing a taskkill like I want it to do, it just opens another instance of notepad.

I know I can do the application shutdown command through vbs, I was mainly just testing with that command to see if I can get it do the typing in the cmd box correctly.



Set WshShell = WScript.CreateObject ("WScript.Shell")
Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")
'==============================================================================================
For Each objProcess in colProcessList
If objProcess.name = "notepad.exe" then
vFound = True
End if
Next
If vFound = True then
WshShell.Run ("C:\Windows\system32\cmd.exe")
WshShell.sendkeys "taskkill /IM notepad.exe"
WshShell.SendKeys "{ENTER}"
Else
Msgbox("Not Found")
End If
 
Why not simply this ?
WshShell.Run "taskkill /F /IM notepad.exe"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
[Set WshShell = WScript.CreateObject ("WScript.Shell")
Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")
'==============================================================================================
For Each objProcess in colProcessList
If objProcess.name = "notepad.exe" then
vFound = True
End if
Next
If vFound = True then
WshShell.Run ("C:\Windows\system32\cmd.exe")
' I Add sleep to fix the problem :d
wscript.sleep 50
WshShell.sendkeys "taskkill /IM notepad.exe"
wscript.sleep 50
WshShell.SendKeys "{ENTER}"
Else
Msgbox("Not Found")
End If
 
That's awesome, that fixed my problem with it writing to the command line, didn't think about putting a sleep command in there... thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top