Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

We use NAnt extensively for our build system. Recently, I've written a couple PowerShell Cmdlets to perform a few database related things. At first, the intent of these Cmdlets was not to necessarily run within our build process. However, this has recently become a need and we would like to run a few of these Cmdlets from our NAnt based build process.

These Cmdlets are written in C# and we have a SnapIn for them (if that matters at all).

A few ideas:

  • Use the exec task to call PowerShell? (not sure how this would work though)
  • Write a custom NAnt task that references and uses the Cmdlet?

What might be a good way to do this?

like image 333
Scott Saad Avatar asked Aug 12 '09 18:08

Scott Saad


2 Answers

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>
like image 129
VJ M Avatar answered Nov 29 '22 14:11

VJ M


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.

like image 23
tomasr Avatar answered Nov 29 '22 13:11

tomasr