同时运行多个执行

时间:2017-12-01 04:16:39

标签: powershell

我正在尝试整理一个“便利”脚本。它已经发展到我实际需要它同时执行多个事物的程度。

基本上,我需要它在单独的命令提示符窗口中启动两个开发Web服务器。这是可以通过PowerShell脚本完成的吗?如果是这样,怎么样?

我正在使用Windows 10附带的任何PowerShell版本。

$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
    }

1 个答案:

答案 0 :(得分:2)

只需使用Start-Process(或其别名start):

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

另一种选择是以background jobs启动服务器进程:

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

后者会在后台运行流程,因此它们的输出不会显示在控制台中,但您可以通过Receive-Job获取它:

Receive-Job -Job $job