Have Powershell process a list of objects from a C# program

I want to add a facility in a c# application where i can:
 
1) Create a list of objects
2) Take that list and pass it to a powershell script from inside my c# appliaction
3) Have the powershell script make changes to the the list of objects it was passed .
4) Return that list of objects back to c#
 

I have an external class called Message.  
    public class Message
    {
        public String name { get; set; }
        public String from { get; set; }
        public String to { get; set; }
        public String date { get; set; }
        public String subject { get; set; }
        public String body { get; set; }
    }
 

Open in new window


I populate the PSDataCollection list class as such:
 
 
         PSDataCollection<Message> mlist = new PSDataCollection<Message>()
          {
              new Message {  to="user1", from="user2", date = "1/10/2010 12:00:00 AM EST", subject = "hi there" , body = "hi again" },
              new Message {  to="user1", from="user3", date = "1/10/2010 12:00:00 AM EST", subject = "new messages" , body = "new messages" }
          }
  

Open in new window


In the powershell script we want it to

    1) read each object
    2) adjust the date field by adding 2 hours to it.
 
Implementation issues:
The following code is our attempt at getting it working.  The first issue we hit was how to import the Message class from an external DLL.  

We tried this:  Add-Type "G:\testBAL\bin\Debug\testBAL.dll" but  got errors
 
Any help would be appreciated.
 

namespace TestProject
{
    class Program
    {
      static void Main(string[] args)
        {
            PSDataCollection<Message> mlist = new PSDataCollection<Message>()
            {
                new Message {  to="user1", from="user2", date = "1/10/2010 12:00:00 AM EST", subject = "hi there" , body = "hi again" },
                new Message {  to="user1", from="user3", date = "1/10/2010 12:00:00 AM EST", subject = "new messages" , body = "new messages" }
            };
            mlist.Complete();
            
            PowerShell ps = PowerShell.Create()
                .AddScript("Add-Type G:\testBAL\bin\Debug\testBAL.dll")
                .AddCommand("Select-Object");
                         
            IAsyncResult async = ps.BeginInvoke<Message>(mlist);
            
            foreach(PSObject result in ps.EndInvoke(async))
            {
                String to = ((Message)(result.BaseObject)).to;
                Console.WriteLine("to=" + to);
            }
        }
    }
}

Open in new window

cdesk458Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cdesk458Author Commented:
I got the question answered on stackoverflow

var mlist = new PSDataCollection<Message>() 
{ 
    new Message {  to="user1", from="user2", date = "1/10/2010 12:00:00 AM", subject = "hi there" , body = "hi again" }, 
    new Message {  to="user1", from="user3", date = "1/10/2010 12:00:00 AM", subject = "new messages" , body = "new messages" } 
};

var rs = RunspaceFactory.CreateRunspace();
rs.Open();
rs.SessionStateProxy.SetVariable("list", mlist);
var ps = PowerShell.Create();
ps.Runspace = rs;

ps.AddScript(@"
    $list | ForEach {
        $_.date = (Get-Date($_.date)).AddHours(2)
    }   
");

ps.Invoke();

var result = rs.SessionStateProxy.GetVariable("list") as PSDataCollection<Message>;
foreach (var item in result)
{
    Console.WriteLine(item.date);
}
rs.Close();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.