Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让Powershell处理C程序中的对象列表_Powershell - Fatal编程技术网

让Powershell处理C程序中的对象列表

让Powershell处理C程序中的对象列表,powershell,Powershell,我想在c应用程序中添加一个工具,我可以: 1获取一组对象,并从我的c应用程序内部将其传递给powershell脚本 2让powershell脚本更改其传递的对象列表 3将对象列表返回给c 我有一个名为Message的外部类 public class Message { public String name { get; set; } public String from { get; set; } public String to

我想在c应用程序中添加一个工具,我可以:

1获取一组对象,并从我的c应用程序内部将其传递给powershell脚本

2让powershell脚本更改其传递的对象列表

3将对象列表返回给c

我有一个名为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; } 
    } 
我填充PSDataCollection列表类,如下所示:

 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" } 
          } 
在powershell脚本中,我们希望它 我读每一个物体 2通过向日期字段添加2小时来调整日期字段

实施问题:

下面的代码是我们试图让它工作的代码。我们遇到的第一个问题是如何从外部DLL导入消息类

我们尝试了以下操作:添加类型G:\testBAL\bin\Debug\testBAL.dll,但出现错误

任何帮助都将不胜感激

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); 
            } 
        } 
    } 
}

根据您的描述,您似乎缺少-Path开关


直接传递DLL时,必须指定-Path。请参阅此处的参考文档

我已将C程序集加载到PowerShell脚本中,并按如下方式使用它:

$dllPath = "C:\path\library.dll"
$tmp = [Reflection.Assembly]::LoadFile($dllPath)
$obj = new-object Namespace.LibraryClass()

您可以使用PowerShell运行空间在.NET应用程序中创建的PowerShell会话中设置和获取变量。我编辑了您的日期参数并删除了EST,以便PowerShell的Get-date cmdlet可以轻松解析它

道格

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();