powershell - 可以选择从powershell将参数传递给MSBuild

标签 powershell msbuild

在我的Build.proj文件中,我具有以下属性:

<DbName Condition="$(DbName) == ''">MyDB</DbName>
<ConnectionString Condition="$(ConnectionString) == ''">"Data Source=localhost;Initial Catalog=$(DbName);Integrated Security=SSPI;MultipleActiveResultSets=True"</ConnectionString>

我有一个PowerShell函数,如下所示:
function Update-Database
{
    param(
        $DbName=""
    )
    msbuild $MSBuildFile /maxcpucount /verbosity:Minimal /target:DatabaseUpdate /p:DbName=$DbName
}

在PowerShell中运行该函数,我得到以下结果:
  • Update-Database MyDB:完美工作
  • Update-Database:失败。在这种情况下,MSBuild不使用默认的“MyDB”。而是使用''

  • 如果我修改此行:
    msbuild $MSBuildFile /maxcpucount /verbosity:Minimal /target:DatabaseUpdate /p:DbName=$DbName
    

    对此:
    msbuild $MSBuildFile /maxcpucount /verbosity:Minimal /target:DatabaseUpdate
    

    运行“UpdateDatabase”使用默认的“MyDB”,一切都很好。

    如果我想选择使用默认值或传递新值,是否必须将整个msbuild...命令包装在if else中?还是有条件地传递参数的方法?

    更新

    我试图避免在ps函数中设置默认值。基本上,我想没有if的情况下模拟此行为:
    function Update-Database
    {
        param(
            $DbName=""
        )
        if($DbName -eq "")
        {
            msbuild $MSBuildFile /maxcpucount /verbosity:Minimal /target:DatabaseUpdate
        }
        else
        {
            msbuild  $MSBuildFile /maxcpucount /verbosity:Minimal /target:DatabaseUpdate /p:DbName=$DbName
        }
    }
    

    最佳答案

    您在命令行中指定的属性变为global property,它将优先于文件中指定的属性。您可以定义一个新属性来避免这种行为,如下所示:

    <_DbName>$(DbName)</DbName>
    <_DbName Condition="$(_DbName) == ''">MyDB</_DbName>
    <ConnectionString Condition="$(ConnectionString) == ''">"Data Source=localhost;Initial Catalog=$(_DbName);Integrated Security=SSPI;MultipleActiveResultSets=True"</ConnectionString>
    

    关于powershell - 可以选择从powershell将参数传递给MSBuild,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24890541/

    相关文章:

    powershell - 为什么PowerShell.exe 没有办法dot source 一个脚本?

    powershell - 如何使用 ansible 读取 powershell 脚本返回的值

    powershell - 使用 powershell 命令处理用户名中的撇号(单引号) - Get-Azureaduser

    c++ - 动态确定构建/触发事件的所有解决方案、项目和配置,并使用 MSBuild 从命令行/脚本调用它们

    c# - 构建错误: Reference PresentationCore could not be resolved

    powershell - 将与模式匹配的行从文件夹中的所有文本文件提取到单个输出文件

    c# - MSBuild 无法在 Azure 云服务 csdef 文件中找到目录

    asp.net - 在开发周期中监控或加速 ASP.NET 编译的提示

    svn - 持续集成和自动版本标记的工作流 : . NET 项目 + CC.NET + SVN + MSBuild

    powershell - 使用 Copy-Item 复制文件夹 - 不同的行为