【问题标题】:A positional parameter cannot be found that accepts argument 't'找不到接受参数“t”的位置参数
【发布时间】:2019-12-23 11:49:21
【问题描述】:

我收到以下错误

New-AzResourceGroup : A positional parameter cannot be found that accepts argument 't'.
At line:1 char:1
+ New-AzResourceGroup -Name @rgName -Location @location -Tag @{LoB="pla ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-AzResourceGroup], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGrou
pCmdlet

尝试使用以下代码创建新资源组时。问题出在哪里?

$rgName = "storage-dev-rg"
$location = "eastus"
New-AzResourceGroup -Name @rgName -Location @location -Tag @{LoB="platform"; CostCenter="IT"}

【问题讨论】:

    标签: powershell syntax parameter-passing


    【解决方案1】:

    引用your own answer

    声明的变量应该使用$引用,而不是@

    about_Variables 解释说,为了在 PowerShell 中创建和稍后引用变量,在这两种情况下,您都需要在它们的名称前加上 sigil $;即 $rgName$location 在您的情况下。

    如果你想执行 splatting,你只需要在变量名前加上 sigil @(参见 about_Splatting)。

    (符号@ 也有其他用途,即@(...),数组子表达式运算符和@{ ... },散列表文字,也用于您的命令。 )

    Splatting 用于将存储在变量中的类似数组的值作为单独的位置参数传递,或者更典型地是将包含参数名称-值对的哈希表的条目绑定到如此命名的参数 - 请参阅this answer

    由于您的变量包含字符串,并且字符串可以被视为类似数组的字符集合(通过System.Collections.IEnumerable 接口),将string 变量有效地将每个字符作为单独的位置参数传递

    PS> $foo = 'bar'; Write-Output @foo # same as: Write-Output 'b' 'a' 'r'
    b
    a
    r
    

    至于你尝试了什么

    -Name @rgName,基于包含字符串'storage-dev-rg'$rgName,将's'(仅第一个字符)传递给-Name,其余字符作为单独的位置参数。 't',第二个字符,是第一个这样的位置参数,由于 New-AzResourceGroup 没有预料到任何 位置 参数,它抱怨它。

    【讨论】:

      【解决方案2】:

      我想通了。声明的变量应使用$ 引用,而不是@

      【讨论】:

        猜你喜欢
        • 2016-07-06
        • 2014-09-30
        • 1970-01-01
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多