Reputation: 431
I wrote a small PowerShell cmdlet in C# that is expecting a parameter of a Dictionary. How do I structure a HashTable (or other object) in a PowerShell script so that I can pass it as a parameter to my cmdlet?
I've tried creating a HashTable in my PowerShell script as follows:
$dic = @{
"Key1" = 1,2,3;
"Key2" = "A","B","C";
}
And then piping this to my cmdlet:
$props | Add-Properties
My cmdlet:
public class AddProps : PSCmdlet {
[Parameter(ValueFromPipeline = true, Mandatory = true)]
public Dictionary<string, object[]> Props {get; set;}
protected override void ProcessRecord() {
DoSomethingWithProps(Props);
}
}
I expected to be able to write a short PowerShell script like this:
$dic = @{
"Key1" = 1,2,3;
"Key2" = "A","B","C";
}
$props | Add-Properties
However, when I try this I get this error:
The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input
I checked the type of $props
and it is indeed a HashTable
. After looking at other questions related to this pipeline input issue and how to build a HashTable
in PowerShell, I am still at a loss for what is going wrong here.
Upvotes: 4
Views: 732
Reputation: 19684
You can solve this using interfaces instead if you don't want to change the type to Hashtable
:
PS> [hashtable].GetInterfaces()
IsPublic IsSerial Name
-------- -------- ----
True False IDictionary
True False ICollection
True False IEnumerable
True False ISerializable
True False IDeserializationCallback
True False ICloneable
[Parameter(ValueFromPipeline = true, Mandatory = true)]
public IDictionary Props { get; set; }
Upvotes: 5