【问题标题】:Unable to pass parameters in powershell while deploying ARM Template from Blob Storage从 Blob 存储部署 ARM 模板时无法在 powershell 中传递参数
【发布时间】:2020-12-31 10:33:37
【问题描述】:

我是新来的。希望有人可以帮助我。

我正在使用 New-AzResourceGroupDeployment cmdlet 通过 Azure arm 模板部署基本网络接口(作为测试)。我正在使用一个 json 模板文件和一个 json 参数文件:

createNic.json & createNic.parameters.json

从我的电脑本地引用两个 json 文件时,我能够成功传递 NicName 参数并使用 NicName 创建 networkInterfaces。

像这样:

New-AzResourceGroupDeployment `
-Name 'CreateNic' `
-ResourceGroupName 'TestRG' `
-TemplateFile 'C:\json\createNic.json' `
-TemplateParameterFile 'C:\json\createNic.parameters.json' `
-NicName 'Test-Nic'

我想通过使用存储 Blob SAS 令牌存储和引用我的 azure blob 存储中的两个 json 文件来部署 networkInterfaces。

只要我不包含,我的 powershell 部署代码就可以工作

-NicName '我的测试网卡'

我的 New-AzResourceGroupDeployment cmdlet 中的参数。当我这样做时,我收到以下错误消息:

New-AzResourceGroupDeployment:找不到与参数名称“NicName”匹配的参数

我还验证了我的 SAS 令牌和 URL 可以通过将它们粘贴到浏览器中来访问。

如果我不包含“-NicName”参数。 networkInterface 使用模板 json 文件中的默认值在我的测试资源组中成功创建。

我的 powershell 部署代码:

$secret = (Get-AzKeyVaultSecret -VaultName 'xxxx-xxx-key-vault' -Name 'xxstorageaccount').SecretValueText
$StorageContext = New-AzStorageContext -StorageAccountName 'xxstorageaccount' -StorageAccountKey $secret

$templateuri = New-AzStorageBlobSASToken `
  -Container 'json-container-templates' `
  -Context $StorageContext `
  -Blob 'createNic.json' `
  -Permission rw `
  -ExpiryTime (Get-Date).AddHours(2.0) -FullUri

Write-Host 'templateuri'
$templateuri

$TemplateParameterUri = New-AzStorageBlobSASToken `
  -Container 'json-container-templates' `
  -Context $StorageContext `
  -Blob 'createNic.parameters.json' `
  -Permission rw `
  -ExpiryTime (Get-Date).AddHours(2.0) -FullUri

Write-Host 'TemplateParameterUri'
$TemplateParameterUri


    New-AzResourceGroupDeployment `
    -Name 'CreateNic' `
    -ResourceGroupName 'TestRG' `
    -TemplateUri $templateuri `
    -TemplateParameterUri $TemplateParameterUri `
    -NicName 'My-Test-Nic'

我的 createNic Json 代码:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "NicName": {
            "type": "String"
        },
        "virtualNetworks_xx_xxx_VNET_externalid": {
            "type": "String"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2020-05-01",
            "name": "[parameters('NicName')]",
            "location": "northcentralus",
            "tags": {
                "displayName": "VM-Nic"
            },
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "privateIPAddress": "10.150.16.36",
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[concat(parameters('virtualNetworks_xx_xxx_VNET_externalid'), '/subnets/xxx-xx-Pool')]"
                            },
                            "primary": true,
                            "privateIPAddressVersion": "IPv4"
                        }
                    }
                ],
                "dnsSettings": {
                    "dnsServers": []
                },
                "enableAcceleratedNetworking": false,
                "enableIPForwarding": false
            }
        }
    ]
}

我的 createNic 参数 Json 代码:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "NicName": {
            "value": "My-Test-Nic"
        },
        "virtualNetworks_xx_xxx_VNET_externalid": {
            "value": "/subscriptions/xxx-xxx-xxx-xxx-xx/resourceGroups/xx-xxx-Networking/providers/Microsoft.Network/virtualNetworks/xx-xxx-VNET"
        }
    }
}

【问题讨论】:

    标签: json azure powershell templates azure-resource-manager


    【解决方案1】:

    如果你看一下here,你可以看到作者传入自定义参数的 PowerShell 的 sn-p。

    $paramObject = @{
        'webAppName' = 'AdbPluralsightWebApp'
        'sqlDbName'  = 'webappdb'
    }
    $parameters = @{
        'Name'                  = 'ContosoWebAppDeployment'
        'ResourceGroupName'     = 'ContosoWebApp'
        'TemplateFile'          = 'C:\\armtemplate.json'
        'TemplateParameterObject'    = $paramObject
        'Verbose'               = $true
    }
    
    New-AzResourceGroupDeployment @parameters
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 2019-12-21
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      相关资源
      最近更新 更多