-1

I'm trying to convert this windows .BAT file (which runs a Client & Server networked code app) into Powershell :

Here is my bat file :

@echo off
setlocal


start cmd

start java FixedMessageSequenceServer

ping 192.0.2.2 -n 1 -w 5000 > null

start /wait java FixedMessageSequenceClient

if errorlevel 1 goto retry

echo Finished successfully
exit

:retry
echo retrying...
start /wait java BatchWakeMeUpSomehow

Here is my Powershell file :

Start-Job -ScriptBlock {
  & java FixedMessageSequenceServer 
  Start-Sleep -s 1
  & java FixedMessageSequenceClient
}




Start-Sleep -s 1

But when I try to run, it doesn't output correctly or do anything. I'm also not sure how to convert the start /wait.

Frode F.
  • 52,376
  • 9
  • 98
  • 114
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
  • @Mofi - I want to have both server and client write to a single window, rather than having output going to two separate CMD windows. I need to then be able to force shutdown this one window( it containing both server and client messages), and then restart it automatically ( [see this question](http://stackoverflow.com/questions/33762588/how-do-i-use-one-java-program-to-monitor-another-java-programs-output) ) – Caffeinated Feb 22 '16 at 06:41

1 Answers1

2

The start external call operator in cmd is roughly equivalent to Start-Process (alias start) in PowerShell - it even has a -Wait parameter.

Start-Job on the other hand launches your scriptjob in a background process.

Start-Process java FixedMessageSequenceServer
Start-Sleep -Seconds 1
$JavaClient = Start-Process java FixedMessageSequenceClient -Wait -PassThru

if($JavaClient.ExitCode)
{
    # exit code is non-zero, better retry
    Start-Process java BatchWakeMeUpSomehow -Wait
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • AFAIK, `Start-Process` does not update `$LASTEXITCODE` variable, so you have to use `-PassThru` and inspect `ExitCode` property of returned `Process` object. – user4003407 Feb 22 '16 at 07:45
  • Ok, so I ran this - but it's not behaving exactly like I want yet. I'd like to be able to type `CTRL+C` to kill the client-server pair while they're both running, then have it restart. curious tho, what is the `-Wait` flag for? – Caffeinated Feb 24 '16 at 04:26