-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.

1
  • @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 ) Commented Feb 22, 2016 at 6:41

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

AFAIK, Start-Process does not update $LASTEXITCODE variable, so you have to use -PassThru and inspect ExitCode property of returned Process object.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.