addohm
addohm

Reputation: 2455

Running multiple executions simultaneously

I am trying to put together a "convenience" script. It's advanced to the point where I actually need it to execute multiple things at once.

Basically, I need it to launch two development web servers in separate Command Prompt windows. Is that something that can be done from a PowerShell script? If so, how?

I'm using whatever PowerShell version that comes with Windows 10.

$ep = Get-ExecutionPolicy

if ($ep -eq 'RemoteSigned') {
    $root = "C:\Users\User\OneDrive\_code_projects\"
    $whichserver = Read-Host -Prompt "Enter the name of the project you'll be working on"
    $test = "y" # Read-Host -Prompt 'Would you like to activate the python environment? y/n'
    $activatestr = ($root + "env\Scripts\Activate.ps1")
    $path = ($root + "django_projects\" + $whichserver + "\")
    $runserverstr = ($path + "\manage.py")

    if ($test -eq 'y') {
        & $activatestr
    }

    $test = Read-Host -Prompt 'Would you like to run the ATOM IDE? y/n'
    if ($test -eq 'y') {
        atom $path
    }

    #$test = Read-Host -Prompt 'Would you like to cd into the project root directory? y/n'
    if ($test -eq 'y') {
        cd $path
    }

    $test = Read-Host -Prompt 'Would you like to run the  dev servers? y/n'
    # Here, I would like to open command prompt either in the root of the react app, or
    # cd into it, then start it. Then I would like to launch the django dev server.
    if ($test -eq 'y') {
        cd $path + $whichserver + "_react_main"
        cmd.exe /C npm start
        cd ..
        cmd.exe /C python.exe $runserverstr runserver
    }

Upvotes: 0

Views: 1216

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200283

Simply use Start-Process (or its alias start):

Start-Process 'python.exe' -ArgumentList $runserverstr, 'runserver'

Another option would be to launch the server processes as background jobs:

$job = Start-Job {
    & 'python.exe' $runserverstr 'runserver'
}

The latter would run the processes in the background, so their output isn't displayed in the console, but you can fetch it via Receive-Job:

Receive-Job -Job $job

Upvotes: 2

Related Questions