Passing a hashtable from C# to PowerShell

Question:

I am attempting unsuccessfully to pass a hashtable from C# to PowerShell as a script parameter. The script and parameters work properly when I execute within PowerShell, so I’m assuming my error is on the C# side.

In C#, I am simply using Command.Parameters.Add() as I would for any other parameter. All the other parameters that I pass to the script are being received correctly, but the hashtable is null.

From the C# side, I have tried using both a Hashtable object and a Dictionary<string, string> object, but neither appears to work. In both cases, I have confirmed that the object is instantiated and has values before passing to PowerShell. I feel like there’s a very obvious solution staring me in the face, but it just isn’t clicking.

Answer:

You can only pass strings as command line parameters.

I don’t know if there a limit but if there isn’t you will need to convert the hashtable to a string and parse it in your PowerShell script.

Source:

Passing a hashtable from C# to PowerShell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply