Assume I have created and opened runspace via
var rs = RunspaceFactory.CreateRunspace();
rs.Open();
Let’s further assume that I want to add a variable into that runspace that is typed as an array by using the New-Variable cmdlet like so:
// create a pipeline to add the variable into the runspace
var pipeline = PowerShell.Create();
// create a command object, add commands and parameters to it...
var cmd = new PSCommand();
cmd.AddCommand("New-Variable");
cmd.AddParameter("Name", "foo");
cmd.AddParameter("Value", "@()");
// associate the command with the pipeline and runspace, and then invoke
pipeline.Commands = cmd;
pipeline.Runspace = rs;
pipeline.Invoke();
The code works and I get no errors, but the variable ‘foo’ is not created as an array type. I’ve tried many different variations on “@()”, but none of them have panned out thus far. Ultimately, I think the question boils down to how to properly format the Value argument to New-Variable so that ‘foo’ will be interpreted as an empty PowerShell array type.
Thanks,
Matt
PSCommand.AddParametertakes a string for the parameter name, and an object for the parameter value. See the docs here.You should put a “real” empty array, not a string representing the powershell script equivalent.