2

I use the below script to run two programs in parallel (line 3 and line 5; line 1 just makes the file open PowerShell, execute the commands and close it again). Unfortunately, the script pauses at line 3 and will not run the second program until I close the first one.

File start.cmd:

@PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=@(^&{$args} %*);'+[String]::Join(';',(Get-Content '%~f0') -notmatch '^^@PowerShell.*EOF$')) & goto :EOF
Set-Location -LiteralPath "D:\ngrok"
.\ngrok tcp --region=us --remote-addr=9.tcp.ngrok.io:22648 22
Set-Location -LiteralPath "C:\Users\apeal\OneDrive\Desktop\SSC"
Start-Process -FilePath "run.bat"

Here is a video of me running the code: https://youtu.be/1FBJTcBXWTM

As you can see, when I run the CMD file, the tcp portal connects, but only after I exit the console using (cmd + c), the portal closes and the server starts.

How can I run both programs with my script in parallel/asynchronous?

0

1 Answer 1

3

A direct invocation like .\ngrok tcp --region=us --remote-addr=9.tcp.ngrok.io:22648 22 will make your script wait until the process is finished. Start-Process in contrast, will not wait by default (you can let it wait with the -Wait switch though).

To pass arguments to the executable that you want to run with Start-Process, you can use the -ArgumentList parameter. So, just replace your direct invocation by:

Start-Process .\ngrok -ArgumentList "tcp --region=us --remote-addr=9.tcp.ngrok.io:22648 22"

and you are good to go.

Sign up to request clarification or add additional context in comments.

Comments

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.