如何在psake中正确使用-parameters和-properties?

sam*_*amy 4 psake

我有以下psake脚本

properties {
    $ApplicationName = "test"
    $ApplicationPath = "c:\this\is\$ApplicationName"
}

Task test {
    "ApplicationName = $ApplicationName"
    "ApplicationPath = $ApplicationPath"
}
Run Code Online (Sandbox Code Playgroud)

我想只将ApplicationName传递给脚本,以避免键入整个应用程序路径.但是当我使用-parameters标志时,不会对属性应用任何更改

Invoke-psake .\script.ps1 -parameters @{ApplicationName = "another_test"} test

ApplicationName = test
ApplicationPath = c:\this\is\test
Run Code Online (Sandbox Code Playgroud)

这听起来不对,因为应该在任何属性块之前评估参数.当我使用该-properties标志时,应用程序名称被更改,但不是路径

Invoke-psake .\script.ps1 -properties @{ApplicationName = "another_test"} test

ApplicationName = another_test
ApplicationPath = c:\this\is\test
Run Code Online (Sandbox Code Playgroud)

所以属性已经初始化了,但不应该-parameters覆盖这种行为?

小智 7

问题是您希望在属性块之前评估参数,但在psake中,属性覆盖参数.

https://github.com/psake/psake/wiki/How-can-I-pass-parameters-to-my-psake-script%3F

properties {
  $my_property = $p1 + $p2
}
Run Code Online (Sandbox Code Playgroud)

构建脚本中的"properties"函数可以覆盖传递给Invoke-psake函数的参数.在上面的示例中,如果参数哈希表是@ {"p1"="v1";"p2"="v2";"my_property"="hello"},则$ my_property仍将最终设置为"v1v2".

我不确定你是否可以覆盖属性并根据该属性更新进行另一个属性更新而不修改psake.你能做的就是创建一个在需要时评估路径的函数:

Function ApplicationPath {"c:\this\is\$ApplicationName"}
Run Code Online (Sandbox Code Playgroud)