Implement a PowerShell pipeline with a script parameter in C#

I have a powershell script that I am trying to execute from C#. However, I get an error:
Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName,Alias,Database,ServerName,PrimarySmtpAddress, @{Name=""EmailAddresses"";Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq ""smtp""} | ForEach-Object {$_.SmtpAddress}}}

Open in new window

Just running Get-Mailbox -Resultsize Unlimited works, but I need greater control over the email addresses, hence the script parameter.

The error I get is:  Script block literals are not allowed in restricted language mode or a Data section.
LVL 31
MlandaTAsked:
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.

MlandaTAuthor Commented:
Michael, thanks for you input :) I am using the code below (I have only included the relevant snippet):
//After we have done this, we can now do the remote calls that will be executed on the server side as script blocks.
powershell = PowerShell.Create();
command = new PSCommand();
command.AddScript("Invoke-Command -ScriptBlock { Get-Mailbox -ResultSize Unlimited } -Session $ra");
//command.AddScript("Invoke-Command -ScriptBlock { Get-Mailbox -ResultSize Unlimited |Select-Object DisplayName,Alias,Database,ServerName,PrimarySmtpAddress, @{Name=\"EmailAddresses\";Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq \"smtp\"} | ForEach-Object {$_.SmtpAddress}}} } -Session $ra");
powershell.Commands = command;
powershell.Runspace = myRunSpace;

Open in new window

What's interesting is that the call to command.AddScript("Invoke-Command -ScriptBlock { Get-Mailbox -ResultSize Unlimited } -Session $ra"); works and I get results. It's just when I try to execute the more complex PowerShell script which get more refined data back that I get the issues. the EmailAddress column returned by this working version includes multiple SMTP address alternatives and other detail which I do not need. The more complex PowerShell script strips out these other details. The complex PowerShell script works when I run it from PowerShell ISE. I only get issues when I run it from C#, but I am not sure how one constructs that pipeline from C#.
Michael FowlerSolutions ConsultantCommented:
Sorry for the delay.

You will need to use the pipeline class

I haven't done this myself but here is an example I found

dir | select -first 3

Open in new window


  Runspace runspace = RunspaceFactory.CreateRunspace();
  runspace.Open();
  Pipeline pipeline = runspace.CreatePipeline();
  Command dir = new Command("dir");
  pipeline.Commands.Add(dir);
  Command select = new Command("select");
  select.Parameters.Add("first", 3);
  pipeline.Commands.Add(select);

Open in new window

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
Exchange

From novice to tech pro — start learning today.