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

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.

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 somethingsleep -Seconds 5# If the process is still active kill itif (!$process.HasExited) {
$process.Kill()
}

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$stdout=$process.StandardOutput.ReadToEnd()
$stderr=$process.StandardError.ReadToEnd()

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.

[…] Capture output from command line tools with PowerShell – 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 …… […]