How do I monitor a process launched from batch files or Powershell scripts?
Batch files are often used to setup an environment prior to starting an executable. You can also do this with Powershell scripts.
If you use batch files and/or Powershell scripts to do this, how do test for memory leaks in the program that is launched by the batch file?
Or how do you choose which program to monitor if the batch file launches more than one executable?
Our Validator tools can do these things, whether it’s memory leaks, code coverage, performance profiling, deadlock detection or execution tracing. In this article I’m going to show you how to do this for batch files, and then afterwards we’ll briefly cover Powershell scripts, as the technique is identical for both batch files and Powershell scripts.
Monitoring a child process
Consider a batch file that looks like this:
@echo off REM start 3 different programs E:\om\c\coverageValidator\examples\nativeExample\DebugNonLink10_0\nativeExample.exe E:\om\c\memory32\examples\nativeExample\DebugNonLink10_0\nativeExample.exe E:\om\c\performanceValidator\examples\nativeExample\DebugNonLink10_0\nativeExample.exe
This batch file starts three different programs. It’s called e:\testMultipleEXE.bat. We’ll use this example batch file to show how to monitor an executable launched from a batch file. We’ll use Coverage Validator for this example, but the process is identical for all Validators. What you learn with one tool you can take to the other tools.
- Launch the sample application. Click on the launch icon on the toolbar.
- The Launch Native or Service dialog is displayed.
- Click Launch Native and .Net Applications. The Launch Native/.Net application dialog is displayed.
- Select the e:\testMultipleEXE.bat using the Browse… button next to the Application to launch field. When you do this the Application to Monitor field is populated the names of the executables in the batch file (or Powershell script). Choose the application that you want to monitor using the Application to Monitor field. In the example below it is E:\om\c\coverageValidator\examples\nativeExample\DebugNonLink10_0\nativeExample.exe
- Click the Launch button to launch the application.
- The batch file is executed using cmd.exe (or the Powershell script is executed using powershell.exe) and the application that is launched that matches the Application to Monitor field is monitored
- Whilst Coverage Validator is instrumenting the application various progress dialogs are displayed. The display of Coverage Validator updates to show the names of source code files and statistics about each file.
- Close the launched application.
- The application closes. Coverage Validator processes any remaining data and displays the final results.
- The coverage results are displayed on the Summary, Coverage, Branch Coverage, Functions, Directories, DLLs and Files and Lines tabs. These have been discussed in previous tutorials, we will not repeat that discussion here.
You can use the same technique in Bug Validator, Memory Validator, Performance Validator and Thread Validator.
What about Powershell scripts?
The process for batch files and Powershell scripts is identical.
However, you may need to modify your Powershell execution policy prior to using Powershell scripts to launch programs.