用PowerShell命令一次启动多个前后端应用

  前言

  最近再做一个POC,有两个后端API和一个前端,每次运行好麻烦,然后就写了个ps1文件,点击执行。

  正文

  直接上代码吧,大家可以参考

<#
同时启动两个NetCore后端 + React前端,所有日志在同一个窗口
#>

# 项目路径配置,根据你本地路径修改
$api1Path = "C:\Source Code\SmartFlow\SmartFlow.API"
$api2Path = "C:\Source Code\SmartFlow\SmartFlow.AI"
$reactPath = "C:\Source Code\SmartFlow\SmartFlow.Web"

Write-Host "===== Start the solution =====" -ForegroundColor Cyan

# 定义并行任务
$jobs = @()

# 任务1:API1
$jobs += Start-Job -ScriptBlock {
    Set-Location $using:api1Path
    dotnet watch run
} -Name "Api1"

# 任务2:API2
$jobs += Start-Job -ScriptBlock {
    Set-Location $using:api2Path
    dotnet watch run
} -Name "Api2"

# 任务3:React前端
$jobs += Start-Job -ScriptBlock {
    Set-Location $using:reactPath
    npm run dev
} -Name "React"

Write-Host "All Services have been started (Ctrl+C Stop All Services)" -ForegroundColor Green

# 持续输出所有Job日志到当前窗口
try
{
    while ($true)
    {
        foreach ($job in $jobs)
        {
            Receive-Job -Job $job
        }
        Start-Sleep -Milliseconds 300
    }
}
finally
{
    Write-Host "Stopping All Services..." -ForegroundColor Yellow
    Get-Job | Remove-Job -Force
}

  然后,又写了一个bat命令,点击启动上面的ps1

@echo off
powershell -ExecutionPolicy Bypass -File "%~dp0start.ps1"

  结束语

  能偷懒的时候,一定要学会偷懒,hhhh

posted @ 2026-09-01 20:49  霖雨  阅读(15)  评论(0)    收藏  举报