Re: Start and Stop a program
- From: "James Garringer" <jgarringer@xxxxxxxxx>
- Date: 7 Sep 2006 18:42:22 -0700
Hi Kris,
Here is a solution that may work for your needs. You will need to
edit the first line of the script changing the parameters for the
RestartProgram routine call. Set the computer name, process name, and
path to your executable (for restarting) all in quotes.
If the target process is not running on the server nothing
happens. If the process is running it will be stopped, and after 5
seconds will restart using the path to the executable passed to the
routine.
----------------
'edit the next line to match your computer name, process and path to
executable
'information; no other editing is necessary below the next line.
RestartProgram ".","Notepad.exe","C:\Windows\System32\Notepad.exe"
Sub RestartProgram(strComputer,strProcess,strPathToExecutable)
'Accepts a computer name, process name, and a path to the executable
'for restarting the application. All parameters are strings.
Dim wshShell
Set wshShell = CreateObject("Wscript.Shell")
If IsProcessRunning (strComputer,strProcess) = True Then
StopProcess strComputer,strProcess
WScript.Sleep 5000 'wait for 5 seconds
wshShell.Run strPathToExecutable
End If
If Err.Number <> 0 Then
wshShell.Popup "There was an error: " & Err.Description,60,"Error"
End If
End Sub
Function IsProcessRunning(strComputer,strProcess)
'Accepts a computer name and process name as string parameters.
'Returns TRUE if the passed process is running, FALSE otherwise.
Dim objWMIService, wshShell
Set objWMIService = GetObject("winmgmts:\\" & strComputer _
& "\root\cimv2")
Set wshShell = CreateObject("Wscript.Shell")
IsProcessRunning = False
On Error Resume Next
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" _
& strProcess & "'")
If Err.Number = 0 Then
If colProcesses.Count <> 0 Then
IsProcessRunning = True
Exit Function
End If
Else
MsgBox "An error occurred: " & Err.Description
End If
End Function
Sub StopProcess (strComputer, strProcess)
'Accepts a computer name and process name as string parameters.
'Stops the passed process.
Dim objWMIService, wshShell
Set objWMIService = GetObject("winmgmts:\\" & strComputer _
& "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" _
& strProcess & "'")
On Error Resume Next
If Err.Number = 0 Then
For Each objProcess in colProcesses
objProcess.Terminate()
Next
Else
MsgBox "An error occurred: " & Err.Description
End If
End Sub
---------------------
Hope this helps,
James Garringer
Kris wrote:
I need to schedule a particular program on my server to start and stop once a
week. The program does not run as a service. It is a program that is in
startup. I simply need the program to automatically close and then reopen.
I figured I would need to write some type of batch file to close the EXE file
that is running in c:\program files\folder and then start it back up.
.
- Follow-Ups:
- Re: Start and Stop a program
- From: Kris
- Re: Start and Stop a program
- Prev by Date: Re: Script to Restart PC at 3:00am everyday
- Next by Date: Re: How to enumerate domains in the forest?
- Previous by thread: Re: CopyFile
- Next by thread: Re: Start and Stop a program
- Index(es):
Relevant Pages
|