Skip to content
Dariel Marlow edited this page Jun 29, 2018 · 1 revision

There are multiple ways of specifying parameters and we'll cover them here.

Let's say your psake script file (e.g. default.ps1) has the following task with parameters:

Task PTest {
    param($p1, $p2, $p3, [switch]$p4) 
    "p1 is $p1"
    "p2 is $p2"
    "p3 is $p3"
    "p4 is $p4"
}

Powershell commandline

PS psake\src> .\psake.ps1 some\path\to\default.ps1 'ptest -p1="hi" -p2="hello world" -p3' -nologo
-------------------------[ptest]-------------------------
p1 is hi
p2 is hello world
p3 is True
p4 is False

psake succeeded executing some\path\to\default.ps1

-------------------------[Build Time Report]-------------------------
Name   Duration
----   --------
PTest  00:00:00.011
Total: 00:00:00.020

Powershell script

Use a script (e.g. build.ps1) to import the psake module and invoke-psake

$psakePath="path\to\psake"
Import-Module "$psakePath\psake.psm1" -Force
$psake.use_exit_on_error = $true

# powershell takes parameters and parses them, thereby losing the actual quoted args when using $args
# for example: .\build.ps1 ptest -p1="hi" -p2="hello world" -p3
# becomes    : ptest -p1=hi -p2=hello world -p3
# So, we use the $myinvocation.line to get the raw string and then strip out the command name to get the pure args

$cleanArgs = $myinvocation.line.Replace($myinvocation.invocationname, '')
Invoke-psake .\default.ps1 "$cleanArgs" -nologo -Framework 4.7

You would then call it as follows:

PS some\folder> .\build.ps1 ptest -p1="hi" -p2="hello world" -p3
-------------------------[ptest]-------------------------
p1 is hi
p2 is hello world
p3 is True
p4 is False

psake succeeded executing .\default.ps1

-------------------------[Build Time Report]-------------------------
Name   Duration
----   --------
PTest  00:00:00.004
Total: 00:00:00.015

Batch

To call from a batch script (build.bat)

@echo off
set psakePath=path\to\psake

set allargs=%*
REM Replace all quotes with escaped quotes so that we retain proper argument values
set cleanargs=%allargs:"=\"%

powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "&{Import-Module '%psakePath%\psake.psm1'; $psake.use_exit_on_error = $true; invoke-psake .\default.ps1 '%cleanargs%' -framework 4.7 -nologo; if ($psake.build_success -eq $false) { exit 1 } else { exit 0 } }" 
exit /b %errorlevel%

Calling it from the command prompt would look like:

some\path>build ptest -p1="hi" -p2="hello world" -p3
-------------------------[ptest]-------------------------
p1 is hi
p2 is hello world
p3 is True
p4 is False

psake succeeded executing .\default.ps1

-------------------------[Build Time Report]-------------------------
Name   Duration
----   --------
PTest  00:00:00.072
Total: 00:00:00.180
Clone this wiki locally