简体   繁体   English

将C#Psobjects的集合传递到Powershell管道

[英]Pass collection of C# Psobjects to powershell pipeline

I have a collection of C# PSObjects that i want to pass to a powershell script. 我有一个要传递给Powershell脚本的C#PSObjects集合。 I like to pass it in the pipeline so i can use it in the process section of the psfile. 我喜欢在管道中传递它,以便可以在psfile的处理部分中使用它。

Anyone have any clue if this is possible or not and in that case. 在这种情况下,任何人都有可能的线索。 How do i do it? 我该怎么做?

Collection<PSObject> colpsObject = new Collection<PSObject>();
PSObject obj1 = new PSObject();
obj1.Properties.Add(new PSNoteProperty("Name1", "Value1"));
obj1.Properties.Add(new PSNoteProperty("Name2", "Value2"));
colpsObject.Add(obj1);

PSObject obj2 = new PSObject();
obj2.Properties.Add(new PSNoteProperty("Name1", "Value1"));
obj2.Properties.Add(new PSNoteProperty("Name2", "Value2"));
colpsObject.Add(obj2);

PowershellRunspace rs = new PowershellRunspace(); //Custom Powershell class
rs.Open();

Command cmd = new Command(m_Script);
cmd.Parameters.Add("Username", m_userName);
cmd.Parameters.Add("Password", m_password);

rs.Add(cmd);
rs.Execute();

I figured it out. 我想到了。 Not sure if it is the best way but it works. 不知道这是否是最好的方法,但它是否有效。

Collection<PSObject> colpsObject = new Collection<PSObject>();
PSObject obj1 = new PSObject();
obj1.Properties.Add(new PSNoteProperty("Name1", "Value1"));
obj1.Properties.Add(new PSNoteProperty("Name2", "Value2"));
colpsObject.Add(obj1);

PSObject obj2 = new PSObject();
obj2.Properties.Add(new PSNoteProperty("Name1", "Value1"));
obj2.Properties.Add(new PSNoteProperty("Name2", "Value2"));
colpsObject.Add(obj2);

PowershellRunspace rs = new PowershellRunspace(); //Custom Powershell class
rs.Open();

Command cmd1 = new Command("param($Param1);$Param1",true);
cmd1.Parameters.Add("Param1", colpsObject);
rs.Add(cmd1);

Command cmd2 = new Command(m_Script);
cmd2.Parameters.Add("Username", m_userName);
cmd2.Parameters.Add("Password", m_password);
rs.Add(cmd2);

rs.Execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM