What's a good way to consume a PowerShell Cmdlet in a NAnt build system? What's a good way to consume a PowerShell Cmdlet in a NAnt build system? powershell powershell

What's a good way to consume a PowerShell Cmdlet in a NAnt build system?


You can use the below exec task in your nant script to call your ps cmdlets.

<exec program="powershell" workingdir="${BuildManagementDir}" verbose="true">    <arg value="-noprofile"/>    <arg value="-nologo"/>    <arg value="-noninteractive"/>    <arg value="-command"/>    <arg value=".\xyz.ps1"/></exec>


You could certainly use the exec task, setting the program attribute to powershell.exe and passing in the command line something like "-Command { }".

Alternatively, you could create a custom NAnt task that internally uses the powershell hosting APIs to execute your cmdlets or scripts. There's a simple example of this (using the PS v1 APIs) here.


Based on JiBe's answer its the other way around, here is the working solution. When running powershell that takes arguments you need to run the powershell script then the arguments.

PS yourscript.ps1 -arg1 value1 -arg2 value2

In NAnt:

<exec program="powershell" workingdir="${powershell_dir}" verbose="true">    <arg value=".\yourscript.ps1"/>        <arg value="-arg1 ${value1}"/>    <arg value="-arg2 ${value2}"/></exec>

The best way I think is to define the arguments in PS for NAnt is like

$value1=$args[0]$value2=$args[1]

So in command line you will use:

PS yourscript.ps1 some_value1 some_value2

Then this translates in NAnt like:

<property name="Value1" value="some_Value1" /><property name="Value2" value="some_Value2" />    <exec program="powershell" workingdir="${powershell_dir}" verbose="true">        <arg value=".\yourscript.ps1"/>            <arg value="${value1}"/>        <arg value="${value2}"/>    </exec>