Creating a pipeline through C # and creating a pipeline with a native powershell script have one significant difference, which is actually pretty subtle: parameter binding.
if I write the version of your code in a pure script, I will get the same error: the hash table literal is treated as a string value.
ps> $ps = $ps.Commands.Add("get-process") ps> $ps = $ps.Commands.Add("select-object") ps> $ps.Commands[1].Parameters.Add("Property", @("Name", '@{N="Foo";E={"Bar"}}'))
In this case, the command receives an array of two lines, a string "name" and a hash table row. This will be broken just like your C #. Now take a look at the correct way to do this (in a script) - let me rewrite line 3:
ps> $ps.Commands[1].Parameters.Add("Property", @("Name", @{N="Foo";E={"Bar"}}))
So what has changed? I removed the quotation marks around the hash table - I pass the hash table as the 2nd element of the array of objects! So, for your C # example to work, you need to do what the parameter binds for us on the command line (that's pretty much!). Replace:
properties.Add("@{N=\"Foo\";E={\"Bar\"}}");
from
properties.Add( new Hashtable { {"N", "Foo"}, {"E", System.Mananagement.Automation.ScriptBlock.Create("\"Foo\"")} } );
I hope this cleanses you. The binding parameter value is probably the least visible, but the most powerful part of the powershell experience.
-Oisin
x0n
source share