A simple task and then again not

A customer asked me if it was possible to grab output from a command and analyze the output afterwards.

In the particular case he needs to call a telnet session and check if there was a proper response from the server.

The easy solution and then again not

The very simple solution would be to start the command from PowerShell, redirecting the output to a file, wait for the process to finish and then read the file content.

But in this case the process would not end on its own, as the telnet server would prompt for username and password.

The complex solution and then again not Smile

Having played around with grabbing output from processes using .NET, I knew that the System.Diagnostics.ProcessStartInfo object was able to handle redirecting of output without using files.

In order to keep a handle on the process after starting it, I would use System.Diagnostics.Process, as that object gives you access to the process during runtime and keeps data after the process exits.

So I fired up PowerShell ISE (I simply love that tool) and started typing away.

# Setup the Process startup info 
$pinfo = New-Object System.Diagnostics.ProcessStartInfo 
$pinfo.FileName = "ping.exe" 
$pinfo.Arguments = "localhost -t" 
$pinfo.UseShellExecute = $false 
$pinfo.CreateNoWindow = $true 
$pinfo.RedirectStandardOutput = $true 
$pinfo.RedirectStandardError = $true

Instead of using a telnet command, I just use a simple ping localhost to simulate a command that generates output, I also added the –t switch to make ping run forever.

To kick off the command, we need a Process object that uses the ProcessStartInfo object.

# Create a process object using the startup info
$process= New-Object System.Diagnostics.Process 
$process.StartInfo = $pinfo

In  some cases the command may exit on its own after doing whatever it does, but in the case of the telnet (and ping with –t) it will not, so we need to handle this, I decided that a 5 second wait would be sufficient in this case. So after waiting for 5 seconds we check to see if the process is still running, if so we just go and kill it.

# Start the process 
$process.Start() | Out-Null 
# Wait a while for the process to do something 
sleep -Seconds 5 
# If the process is still active kill it 
if (!$process.HasExited) 


Now that we are sure that the command is done executing, we can grab the output from stdout and stderr, both are useful when we analyze the output from a command line tool, as some will write only to stdout while others will write normal output to stdout and errors to stderr. Remember to call the method ReadToEnd() and not just assign the value of StandardOutput to your variable. The content from StandardOutput is a stream object that must be read like a file.

# get output from stdout and stderr 

Now that we have the output from the command line, we can start to analyze if things went well or not. In the case of Ping we can look for the text “Reply from” to indicate success.

A little note to remember, the $stdout contains an array of strings, and normally $stdout -contains “Reply to” would return true, but for some reason it doesn’t. However calling the array method contains will correctly return true if the array contains the text “Reply to”. I suspect this to be caused by the fact the array is a .NET object but I am not sure of this.

# check output for success information, you may want to check stderr if stdout if empty 
if ($stdout.Contains("Reply from")) 
	# exit with errorlevel 0 to indicate success 
	exit 0 
} else { 
	# exit with errorlevel 1 to indicate an error 
	exit 1 

The script simply returns 0 if the Reply from text is found, and 1 if not.

You can download the complete script here

[download id=”11203″]