【问题标题】:Azure Powershell Positional Parameter errorAzure Powershell 位置参数错误
【发布时间】:2020-09-18 12:16:54
【问题描述】:

我正在尝试使用 powershell 进行部署。参数和模板文件都存储在 blob 存储中,但我在尝试下载 blob 之前就收到了以下错误。

New-AzResourceGroupDeployment : A positional parameter cannot be found that accepts argument 'newdeployment-878059'. At C:\Temp\New-Deployment\deploy-core.ps1:86 char:1

我使用的代码如下

$vnetRG = "rg-vnet"
$vpnRG = "rg-vpn"
$fwRG = "rg-fw"
$btnRG = "rg-bastion"
$loc = "west europe"

New-AzResourceGroupDeployment -Name = "newdeployment-$random"-ResourceGroupName "rg-vnet" `
  -TemplateParameterFile "$PSScriptRoot\core.parameters.json" -TemplateUri = $templateFileUri `
  -vpnResourceGroupName $rgRG -vpnResourceGroupName $vpnRG -fwResourceGroupName $fwRG  -btnResourceGroupName $btnRG 

我正在尝试将多个资源部署到一个订阅中的各种资源组。

提前致谢:)

【问题讨论】:

  • 去掉参数名后面的=。另外,查看the docs,我在该cmdlet 上看不到vpnResourceGroupNamefwResourceGroupNamebtnResourceGroupName 参数。不仅如此,您还添加了两次vpnResourceGroupName。如果我可以建议,请查看Splatting 参数以获取可以采用大量参数的cmdlet。
  • 谢谢@Theo 不敢相信我做到了。感谢您的帮助,我正在使用 splatting 但将所有内容分散开来试图隔离问题。再次感谢。您能否将您的回复标记为 aswer,我会接受 :)

标签: azure powershell azure-powershell


【解决方案1】:

这是我的评论作为答案

在您的代码中,您在一些参数名称和用于该参数的实际值之间添加了一个= 字符。
在 PowerShell 中,您可以为参数分配一个值,并在两者之间使用空格字符。

此外,该 cmdlet 使用的参数(根据 the docs)不存在,例如 vpnResourceGroupNamefwResourceGroupNamebtnResourceGroupName。对我来说,它们听起来像是被错误地添加为参数的变量,而前导 $ 被剥离了?

对于可能使用大量参数的 cmdlet,我建议使用 Splatting,以保持代码简洁且易于维护。

类似:

$splatParams = @{
    Name                  = "newdeployment-$random"
    ResourceGroupName     = "rg-vnet"
    TemplateParameterFile = "$PSScriptRoot\core.parameters.json"
    TemplateUri           = $templateFileUri
}

New-AzResourceGroupDeployment @splatParams

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多