GithubHelp home page GithubHelp logo

Powershell script task about nantcontrib HOT 10 OPEN

nant avatar nant commented on July 17, 2024
Powershell script task

from nantcontrib.

Comments (10)

patrickfarry avatar patrickfarry commented on July 17, 2024

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.

patrickfarry avatar patrickfarry commented on July 17, 2024

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.

dguder avatar dguder commented on July 17, 2024

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.

patrickfarry avatar patrickfarry commented on July 17, 2024

Do you what the whole solution?

from nantcontrib.

dguder avatar dguder commented on July 17, 2024

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.

rmboggs avatar rmboggs commented on July 17, 2024

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.

patrickfarry avatar patrickfarry commented on July 17, 2024

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.

patrickfarry avatar patrickfarry commented on July 17, 2024

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.

rmboggs avatar rmboggs commented on July 17, 2024

Sounds great.

from nantcontrib.

andrewducker avatar andrewducker commented on July 17, 2024

Did this ever get merged in?

from nantcontrib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.