【发布时间】:2019-02-12 16:11:03
【问题描述】:
我用 C# 编写了一个小型 PowerShell cmdlet,它需要一个字典参数。如何在 PowerShell 脚本中构建 HashTable(或其他对象),以便可以将其作为参数传递给我的 cmdlet?
我尝试在我的 PowerShell 脚本中创建一个 HashTable,如下所示:
$dic = @{
"Key1" = 1,2,3;
"Key2" = "A","B","C";
}
然后将它传送到我的 cmdlet:
$props | Add-Properties
我的 cmdlet:
public class AddProps : PSCmdlet {
[Parameter(ValueFromPipeline = true, Mandatory = true)]
public Dictionary<string, object[]> Props {get; set;}
protected override void ProcessRecord() {
DoSomethingWithProps(Props);
}
}
我希望能够像这样编写一个简短的 PowerShell 脚本:
$dic = @{
"Key1" = 1,2,3;
"Key2" = "A","B","C";
}
$props | Add-Properties
但是,当我尝试这个时,我得到了这个错误:
输入对象不能绑定到命令的任何参数,因为命令不接受管道输入,或者输入及其属性与接受管道输入的任何参数都不匹配
我检查了$props 的类型,确实是HashTable。在查看了与此管道输入问题以及如何在 PowerShell 中构建HashTable 相关的其他问题之后,我仍然不知道这里出了什么问题。
【问题讨论】:
标签: c# powershell