Comments (10)
Sorry - that looks ugly - here is my test build file
<project name="My project" default="build" basedir=".">
<description>The top level build file.</description>
<property name="tmpLog" value="c:\tempSVNUpdate" />
<property name="results" value="Unknown" />
<target name="build" >
<powershell property="results" taskvar = "myvar" runspace="myrunspace">
$var = "this is the script"
$myvar.Properties["tmpLog"] = "Hello"
$myvar.Project.ProjectName
</powershell>
<powershell property="results">
$var = "running from a new runspace"
$var
</powershell>
<powershell property="results" runspace="myrunspace">
$var
</powershell>
</target>
</project>
from nantcontrib.
Here is the source - -
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using NAnt.Core;
using NAnt.Core.Attributes;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace PowershellPlugin
{
[TaskName("powershell" )]
public class PowershellTask : Task
{
private string _runspaceName = string.Empty;
private string _taskVarName = "task";
private bool _isNamedRunspace = false;
private string _property = string.Empty;
static private Dictionary<string, Runspace> _namedSession;
[TaskAttribute("runspace", Required = false)]
[StringValidator(AllowEmpty = false)]
public string RunspaceName
{
get { return _runspaceName; }
set { _isNamedRunspace = true; _runspaceName = value; }
}
[TaskAttribute("taskvar", Required = false)]
[StringValidator(AllowEmpty = false)]
public string TaskVar
{
get { return _taskVarName; }
set { _taskVarName = value; }
}
[TaskAttribute("property", Required = false)]
[StringValidator(AllowEmpty = false)]
public string Property
{
get { return _property; }
set { _property = value; }
}
public PowershellTask()
{
_runspaceName = String.Empty;
if (_namedSession == null)
{
object l = new object();
lock (l)
{
if (_namedSession == null)
{
_namedSession = new Dictionary<string, Runspace>();
}
}
}
}
protected override void ExecuteTask()
{
Runspace runspace = null;
try
{
StringBuilder scriptText = new StringBuilder();
scriptText.Append(XmlNode.InnerText);
object l = new object();
lock (l)
{
if (!_namedSession.TryGetValue(_runspaceName, out runspace))
{
InitialSessionState iss = InitialSessionState.CreateDefault();
SessionStateVariableEntry thisTask = new SessionStateVariableEntry(_taskVarName, this, "The task being called in nant");
iss.Variables.Add(thisTask);
runspace = RunspaceFactory.CreateRunspace(iss);
if (_isNamedRunspace)
{
_namedSession.Add(_runspaceName, runspace);
}
runspace.Open();
}
}
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddScript(scriptText.ToString());
Collection<PSObject> results = ps.Invoke();
StringBuilder resultString = new StringBuilder();
if (_property != String.Empty)
{
foreach (PSObject po in results)
{
resultString.AppendLine(po.ToString());
}
this.Properties[_property] = resultString.ToString();
}
}
finally
{
if (!_isNamedRunspace && runspace != null)
{
runspace.Close();
runspace.Dispose();
}
}
}
}
}
from nantcontrib.
Here is the source - -
(now using github markdown)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using NAnt.Core;
using NAnt.Core.Attributes;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace PowershellPlugin
{
[TaskName("powershell" )]
public class PowershellTask : Task
{
private string _runspaceName = string.Empty;
private string _taskVarName = "task";
private bool _isNamedRunspace = false;
private string _property = string.Empty;
static private Dictionary<string, Runspace> _namedSession;
[TaskAttribute("runspace", Required = false)]
[StringValidator(AllowEmpty = false)]
public string RunspaceName
{
get { return _runspaceName; }
set { _isNamedRunspace = true; _runspaceName = value; }
}
[TaskAttribute("taskvar", Required = false)]
[StringValidator(AllowEmpty = false)]
public string TaskVar
{
get { return _taskVarName; }
set { _taskVarName = value; }
}
[TaskAttribute("property", Required = false)]
[StringValidator(AllowEmpty = false)]
public string Property
{
get { return _property; }
set { _property = value; }
}
public PowershellTask()
{
_runspaceName = String.Empty;
if (_namedSession == null)
{
object l = new object();
lock (l)
{
if (_namedSession == null)
{
_namedSession = new Dictionary<string, Runspace>();
}
}
}
}
protected override void ExecuteTask()
{
Runspace runspace = null;
try
{
StringBuilder scriptText = new StringBuilder();
scriptText.Append(XmlNode.InnerText);
object l = new object();
lock (l)
{
if (!_namedSession.TryGetValue(_runspaceName, out runspace))
{
InitialSessionState iss = InitialSessionState.CreateDefault();
SessionStateVariableEntry thisTask = new SessionStateVariableEntry(_taskVarName, this, "The task being called in nant");
iss.Variables.Add(thisTask);
runspace = RunspaceFactory.CreateRunspace(iss);
if (_isNamedRunspace)
{
_namedSession.Add(_runspaceName, runspace);
}
runspace.Open();
}
}
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddScript(scriptText.ToString());
Collection<PSObject> results = ps.Invoke();
StringBuilder resultString = new StringBuilder();
if (_property != String.Empty)
{
foreach (PSObject po in results)
{
resultString.AppendLine(po.ToString());
}
this.Properties[_property] = resultString.ToString();
}
}
finally
{
if (!_isNamedRunspace && runspace != null)
{
runspace.Close();
runspace.Dispose();
}
}
}
}
}
from nantcontrib.
Do you what the whole solution?
from nantcontrib.
First of all I wanted to see the whole code. Do you have any tests?
Some review:
- Your lock object should be defined at class level.
- What happens if powershell could not be found? (e.g on WinXP or on mono on linux?)
- Some comments for documentation are required (see other nantcontrib source files)
from nantcontrib.
I like the idea. If you are wanting to submit this task to NAntContrib, I would ask if you could submit this via a pull request, details can be found here.
Documentation for this task would also be very helpful. :)
from nantcontrib.
I don' t have anything else. Happy to spend some time bringing it up to
standard.
I will look at the other contrib projects and figure out what to do. Will
send an update when I get it done.
Cheers.
Connected by DROID on Verizon Wireless
-----Original message-----
From: Dominik Guder
<reply+i-2528372-b66055b90339b6ceec05f59b133e8fb0fc42c4db-1258583@reply.githu
b.com>
To: patrickfarry [email protected]
Sent: Mon, Dec 12, 2011 23:01:25 GMT+00:00
Subject: Re: [nantcontrib] Powershell script task (#5)
First of all I wanted to see the whole code. Do you have any tests?
Some review:
- Your lock object should be defined at class level.
- What happens if powershell could not be found? (e.g on WinXP?)
- Some comments for documentation is required (see other nantcontrib source
files)
Reply to this email directly or view it on GitHub:
#5 (comment)
from nantcontrib.
Will do. I got mail from Dominik. I will be upgrading what I did to fit the
project and will get back to you guys.
Connected by DROID on Verizon Wireless
-----Original message-----
From: Ryan Boggs
<reply+i-2528372-b66055b90339b6ceec05f59b133e8fb0fc42c4db-1258583@reply.githu
b.com>
To: patrickfarry [email protected]
Sent: Mon, Dec 12, 2011 23:14:02 GMT+00:00
Subject: Re: [nantcontrib] Powershell script task (#5)
I like the idea. If you are wanting to submit this task to NAntContrib, I
would ask if you could submit this via a pull request, details can be found
here.
Documentation for this task would also be very helpful. :)
Reply to this email directly or view it on GitHub:
#5 (comment)
from nantcontrib.
Sounds great.
from nantcontrib.
Did this ever get merged in?
from nantcontrib.
Related Issues (20)
- Import cvs task from NAnt
- Website updates
- reverse a string list HOT 4
- Add Ssh/Scp Tasks HOT 2
- Which codepage the code used? HOT 1
- NUnit2Report fails to render graphs on modern browsers HOT 1
- VSS task - using old interop dll HOT 7
- nunit2report counts ignored tests as "errors".
- waitforexit cannot capture failures HOT 4
- WaitForExitTask System.Security.SecurityException: Request failed HOT 2
- p4Info task throws System.NullReferenceException - BUG HOT 5
- Cannot Compile On VS2012 HOT 1
- Consider Migrate to .NET 3.5+?
- iisapppool reports "Invalid namespace" HOT 1
- Add support for msbuild.exe v12.0 to the msbuild task HOT 3
- Registry path to vb6.exe incorrect for x64 HOT 1
- Mono build fails at MSI and SourceSafe tasks with mono 3.2.3++
- Amazing spell mistake.
- IIS web tasks do not support sites with secure bindings
- <link> task does not fail on unexisting library
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nantcontrib.