首页 > 解决方案 > 接受可变数量的哈希集作为参数

问题描述

我正在尝试编写一个 powershell 脚本,它将一个字符串作为第一个参数,然后是可变数量的字典。

这是脚本的启动方式:

param
(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string] $Identity,

    [Parameter(Mandatory = $true,
        ValueFromRemainingArguments=$true,
        Position = 1)]
    [ValidateNotNullOrEmpty()]
    [hashtable[]] $Contacts
)

当我尝试执行如下脚本时:

.\scriptName.ps1 "alice@alice" @{email="a"; firstName="fn"} @{email="b";firstName="fn2"}

我收到以下错误:

D:\Griffin\sources\dev\SubstrateSearch\src\TdsDeployment\SetupSubstrateSearchServices\Scripts\CreateExplicitContacts.ps1:无法处理参数“联系人”的参数转换。无法将值“System.Collections.Generic.List`1[System.Object]”转换为类型“System.Collections.Hashtable[]”。错误:“无法将“System.String”类型的“alice@alice”值转换为“System.Collections.Hashtable”类型。” 在 line:1 char:1 + .\CreateExplicitContacts.ps1 "alice@alice" @{email="a"; firstName="f ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [CreateExplicitContacts.ps1], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId :

我究竟做错了什么?

标签: powershell

解决方案


推荐阅读