将命令行参数注入psake

时间:2010-02-05 16:14:12

标签: powershell psake

我想将命令行参数注入我的psake构建脚本,如: 。\ build.ps1部署环境=“开发”

但psake会将每个参数视为一个任务,并回答“任务不存在”

是否可以在psake中注入命令行参数?

build.ps1 -->
Import-Module '.\psake.psm1'
Invoke-psake '.\tasks.ps1' $args
Remove-Module psake

3 个答案:

答案 0 :(得分:10)

latest release of psake现在supports passing parameters来调用psake,例如

Invoke-psake .\parameters.ps1 -parameters @{"p1"="v1";"p2"="v2"} 

刚刚添加了此功能。 :)

答案 1 :(得分:1)

全局变量现在将解决我的问题,并且只有一个对$ global的引用:arg_environent如果我找到更好的方法来注入属性,它将很容易改变。

<强> build.ps1

param(
    [Parameter(Position=0,Mandatory=0)]
    [string]$task,
    [Parameter(Position=1,Mandatory=0)]
    [string]$environment = 'dev'
)

clear
$global:arg_environent = $environment
Import-Module .\psake.psm1 
Invoke-psake tasks.ps1 $task
Remove-Module psake

<强> tasks.ps1

properties {
    $environment = $global:arg_environent
}

task default -depends Deploy

task Deploy {  
   echo "Copy stuff to $environment"
}

答案 2 :(得分:0)

我不是专家,但我认为不可能将参数传递给Invoke-Psake。查看Psake的最新资源,Invoke-Psake函数的参数是:

param(
    [Parameter(Position=0,Mandatory=0)]
    [string]$buildFile = 'default.ps1',
    [Parameter(Position=1,Mandatory=0)]
    [string[]]$taskList = @(),
    [Parameter(Position=2,Mandatory=0)]
    [string]$framework = '3.5',   
    [Parameter(Position=3,Mandatory=0)]
    [switch]$docs = $false    
    )

有4个参数,构建文件,任务列表,.NET框架版本,是否输出任务的文档。我是powershell和psake的新手,我正在尝试做同样的事情,我正在尝试在我的脚本中做这样的事情来实现同样的目的:

properties { 
   $environment = "default"
}

task PublishForLive -precondition { $environment = "Live"; return $true; } -depends Publish {

}

task PublishForStaging -precondition { $environment = "Staging"; return $true; } -depends Publish {

}

task Publish {
    Write-Host "Building and publishing for $environment environment"
    #Publish the project...
}

然后使用PublishForLive或PublishForStaging调用psake,无论我需要什么:

powershell -NoExit -ExecutionPolicy Unrestricted -Command "& {Import-Module .\tools\psake\psake.psm1; Invoke-psake .\psake-common.ps1 PublishForLive }"

但它似乎对我不起作用!在任务前提条件中设置$ environment变量似乎没有任何效果。仍在努力使这项工作......

相关问题