Borderman
Borderman

Reputation: 21

Pass collection of C# Psobjects to powershell pipeline

I have a collection of C# PSObjects that i want to pass to a powershell script. I like to pass it in the pipeline so i can use it in the process section of the 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();

Upvotes: 0

Views: 2163

Answers (1)

Borderman
Borderman

Reputation: 21

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

Upvotes: 1

Related Questions