You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中通过Start-Process向cmd.exe传递多变量命令行失败

PowerShell调用cmd执行msbuild参数传递失败的解决方法

问题原因

报错'C:\Program' is not recognized的核心是带空格的路径未被正确引用。当你通过Start-Process向cmd.exe传递参数数组时,C:\Program Files\...会被cmd拆成C:\Program和Files\...两个独立部分,导致cmd误把C:\Program当作命令执行。

修复方案

方案1:直接拼接完整cmd命令字符串

把msbuild路径用双引号包裹,拼接成完整命令后作为/c的单个参数传递,确保cmd能识别带空格的路径:

$msbuild = "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\msbuild.exe"
$buildCommand = 'C:\Git\Asd\build\XDA_2019.sln /m /t:Build /p:Configuration=Debug /p:Platform="ASX Platform"'
# 给带空格的msbuild路径添加双引号,拼接成完整命令
$fullCmd = "`"$msbuild`" $buildCommand"
Start-Process -FilePath cmd.exe -ArgumentList "/c", $fullCmd -PassThru -Wait -NoNewWindow

方案2:跳过cmd,直接调用msbuild(推荐)

PowerShell可以直接调用外部程序,无需绕cmd,参数传递更可靠:

$msbuild = "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\msbuild.exe"
# 用数组存储msbuild的所有参数,PowerShell会自动处理空格
$buildArgs = @(
    "C:\Git\Asd\build\XDA_2019.sln",
    "/m",
    "/t:Build",
    "/p:Configuration=Debug",
    '/p:Platform="ASX Platform"'
)
# 使用Start-Process执行(适合需要窗口控制等场景)
Start-Process -FilePath $msbuild -ArgumentList $buildArgs -PassThru -Wait -NoNewWindow

# 或者更简洁的直接调用(无需额外窗口控制时用)
& $msbuild @buildArgs

方案3:强制cmd识别完整命令(必须用cmd时)

如果一定要通过cmd执行,需将整个命令作为/c的单个参数,确保所有带空格的部分都被正确引用:

$msbuild = "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\msbuild.exe"
$buildCommand = 'C:\Git\Asd\build\XDA_2019.sln /m /t:Build /p:Configuration=Debug /p:Platform="ASX Platform"'
$cmdArg = "/c `"$msbuild`" $buildCommand"
Start-Process -FilePath cmd.exe -ArgumentList $cmdArg -PassThru -Wait -NoNewWindow

内容的提问来源于stack exchange,提问作者galsi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:13:19