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

PowerShell 7.3调用带参数的可执行文件出现异常

PowerShell 7.3升级后MSBuild参数传递错误解决方法

问题现象

将PowerShell从7.2.7升级至7.3版本后,调用带复杂参数的MSBuild时出现日志加载错误,示例执行代码:

msbuild.exe "$Solution" /maxcpucount:1 `
                /t:Build `
                /p:Configuration=$Configuration `   
                "/l:TeamCity.MSBuild.Logger.TeamCityMSBuildLogger,$RootDir\.build\packages\TeamCity.Dotnet.Integration\build\_common\msbuild15\TeamCity.MSBuild.Logger.dll;teamcity"

执行后报错:

MSBUILD : error MSB1021: Cannot create an instance of the logger. Could not load file or assembly ''JetBrains.BuildServer.MSBuildLoggers.MSBuildLogger\,"D:\\BuildAgentA\\plugins\\dotnetPlugin\\bin\\JetBrains.BuildServer.MSBuildLoggers.4.0.dll"'' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

问题原因

PowerShell 7.3对外部程序的参数解析逻辑做了调整,针对包含逗号、引号的复杂参数,原7.2.7版本的写法会被错误转义,导致MSBuild收到的logger参数格式错乱,无法正确识别程序集路径。

解决方法

方法1:使用数组传递参数(推荐)

将每个MSBuild参数作为数组元素单独定义,PowerShell会逐个传递参数,避免解析错误:

# 定义参数数组
$msbuildParams = @(
    "$Solution",
    "/maxcpucount:1",
    "/t:Build",
    "/p:Configuration=$Configuration",
    "/l:TeamCity.MSBuild.Logger.TeamCityMSBuildLogger,$RootDir\.build\packages\TeamCity.Dotnet.Integration\build\_common\msbuild15\TeamCity.MSBuild.Logger.dll;teamcity"
)

# 调用MSBuild
& msbuild.exe $msbuildParams

方法2:使用停止解析符号--%

在调用MSBuild前添加--%,让PowerShell直接将后续参数原封不动传递给外部程序(注意:--%后的PowerShell变量不会被解析,需提前替换变量值):

# 提前替换变量到参数中
$loggerParam = "/l:TeamCity.MSBuild.Logger.TeamCityMSBuildLogger,$RootDir\.build\packages\TeamCity.Dotnet.Integration\build\_common\msbuild15\TeamCity.MSBuild.Logger.dll;teamcity"

# 使用--%传递参数
msbuild.exe --% "$Solution" /maxcpucount:1 /t:Build /p:Configuration=$Configuration "$loggerParam"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 01:10:11