【问题标题】:Parameterising values causes error in powershell/msbuild command参数化值会导致 powershell/msbuild 命令出错
【发布时间】:2023-03-14 13:59:01
【问题描述】:

我有一个 powershell 脚本,它依次调用 msbuild.exe 并使用解决方案值和参数参数运行它。如果我通过手动编写该行来运行此脚本,它将按预期运行并构建解决方案,但是,由于此脚本旨在构建多个解决方案,参数、解决方案文件路径和 msbuild 路径都作为变量传递.

我需要运行的行如下所示:

& 'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe' FooBar.sln "/t:rebuild" /p:PlatformTarget=AnyCPU /p:Configuration=Release /p:DeployOnBuild=true /p:CustomizablePublishDir=true /p:OutDir=F:\agent\_work\1\s\Deploy.Temp\Foo.Bar.Binaries\\ | Out-Host 

而其拆分成变量的方式如下:

$1 = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe'
$2 = 'FooBar.sln'
$3 = "/t:rebuild /p:PlatformTarget=AnyCPU /p:Configuration=Release /p:DeployOnBuild=true /p:CustomizablePublishDir=true /p:OutDir=F:\agent\_work\1\s\Deploy.Temp\Foo.Bar.Binaries\\ | Out-Host"


& $1 $2 $3

对于上下文,为了获得这些值,我们使用一个包含许多节点的 XML,每个节点有 3 个属性。

我运行参数化脚本时遇到的错误如下:

Unhandled Exception: System.ArgumentException: The name "Foo_Bar:PlatformTarget=AnyCPU /p:Configuration=Release /p:DeployOnBuild=true /p:CustomizablePublishDir=true /p:OutDir=F:\agent\_work\1\s\Deploy.Temp\Foo.bar.Binaries\\ | Out-Host" contains 
an invalid character ".".

显然这是由于 powershell 处理字符串的方式,但我不知道如何使其工作。

【问题讨论】:

    标签: powershell msbuild powershell-5.0


    【解决方案1】:

    | Out-Host 不应该作为参数的一部分传递给应用程序 - 但这不是这里的问题。

    这似乎是将所有命令行开关组合在一个字符串中并在所述参数中的任何位置使用. - 如果我将字符串拆分为单独的部分或将您的 OutDir 参数替换为不t 包含任何.msbuild 继续尝试构建项目:

    $msbuild = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe'
    $solution = 'FooBar.sln'
    $arguments = "/t:rebuild /p:PlatformTarget=AnyCPU /p:Configuration=Release /p:DeployOnBuild=true /p:CustomizablePublishDir=true /p:OutDir=F:\agent\_work\1\s\Deploy.Temp\Foo.Bar.Binaries"
    
    &$msbuild $solution $arguments.Split(' ') |Out-Host
    

    【讨论】:

    • 您能解释一下为什么不应将| Out-Host 传递给应用程序吗?如果不应该,为什么它会起作用?使用$argument.Split(' ') 似乎有效!非常感谢您的帮助。
    • 因为Out-Host 是一个powershell cmdlet,不是msbuild 的有效参数。
    猜你喜欢
    • 2019-04-17
    • 2015-07-29
    • 1970-01-01
    • 2017-07-27
    • 2021-07-05
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多