【问题标题】:Override properties when including a psake script in an other psake script在其他 psake 脚本中包含 psake 脚本时覆盖属性
【发布时间】:2012-10-14 15:23:34
【问题描述】:

我是 psake 的新手,我遇到了这个问题:我有 2 个 psake 脚本:

(1):base_tasks.ps1:

properties{ 

$a = "hello"

$b = "hi"

}

task One{
  Write-Host $a
}

(2): install.ps1

Include .\base_tasks.ps1

properties{ 

$a = "Goodbye"

$b = "Adjeu"

}

task default -depends One

现在,是否可以覆盖文件 1 中的属性和变量?我想将文件 1 用作“基本任务”并在 install.ps1 中使用这些任务并覆盖属性。还是我必须以其他方式做到这一点?我将调用 install.ps1 并使用 install.ps1 中的 $a 和 $b。

  • DanceAlot

【问题讨论】:

标签: powershell properties build task psake


【解决方案1】:

source看来Properties只是一个函数:

function Properties {
    [CmdletBinding()]
    param(
        [Parameter(Position=0,Mandatory=1)][scriptblock]$properties
    )
    $psake.context.Peek().properties += $properties
}

所以当你再次调用它时,它只会再次添加属性。

然后将属性转换为变量,如下所示:

foreach ($key in $properties.keys) {
        if (test-path "variable:\$key") {
            set-item -path "variable:\$key" -value $properties.$key | out-null
        }
    }

【讨论】:

  • 哦,好的。我明白了,谢谢你澄清这个问题。所以有没有办法实现我所需要的?还是我使用 psake 的方式错误:)
猜你喜欢
  • 2011-05-01
  • 1970-01-01
  • 2011-09-21
  • 2016-05-19
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多