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.

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.

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.

 

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.

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.

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

You can download the complete script here

Download “CaptureCommandOutput” CaptureCommandOutput.zip – Downloaded 947 times – 639 B