在我的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/