The other day, I had to restart a Windows server using only Powershell. The catch was that I had to do it only if the output of a command line tool was a certain way. I also kept a log for documentation purposes.

This script shows how easy it was.

$oascmd = "C:\path\to\oascmd.exe"$output = & $oascmd # Check if the output contains the string "alive" if($output -match "alive") {
Restart-Service -Name "Station_xl" -ErrorAction SilentlyContinue

$time = Get-Date -Format "MM/dd/yyyy HH:mm:ss"$log = "$time - Service Station_xl stopped and started. Output:$output"