0

我有一个 C# PSObjects 集合,我想将它们传递给一个 powershell 脚本。我喜欢在管道中传递它,这样我就可以在 psfile 的进程部分中使用它。

任何人都知道这是否可能,在这种情况下。我该怎么做?

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

1 回答 1

1

我想到了。不确定这是否是最好的方法,但它有效。

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();
于 2012-09-18T16:05:11.347 回答