Richard
Richard

Reputation: 1739

Powershell statement in C#

Is there a way to do this PowerShell statement in C#

$myvariable = New-PSSessionOption -SomeParameter -AnotherParameter -YetAnotherParameter

I know how to run commands and add parameters (PowerShell.AddCommand etc), but I'm missing something that lets me do an entire statement.

The docs do suggest there is an 'AddStatement' method on the PowerShell class, but it's not there in reality.

Or am I better just putting it all in a script and just calling that from my C# instead?

Thanks

Upvotes: 3

Views: 1267

Answers (1)

x0n
x0n

Reputation: 52420

The AddStatement is only in the PowerShell v3 CTP (which is not production ready yet)

It's actually simpler than you think. That line should be treated as a script. A script doesn't neccessarily mean a physical ps1 file. Use the AddScript method:

var ps = PowerShell.Create();
ps.Commands.AddScript("$myvariable = New-PSSessionOption ...");
ps.Invoke();

Upvotes: 2

Related Questions