从 Blob 存储部署 ARM 模板时无法在 powershell 中传递参数

Unable to pass parameters in powershell while deploying ARM Template from Blob Storage

我是新来的。希望有人能帮我解决这个问题。

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

createNic.json & createNic.parameters.json

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

像这样:

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 文件来部署网络接口。

只要我不包含

,我的 powershell 部署代码就可以工作

-昵称'My-Test-Nic'

我的 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'

我的createNicJson代码:

{
    "$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"
        }
    }
}

如果您看一下 here,您可以看到作者在其中传递自定义参数的 PowerShell 片段。

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

New-AzResourceGroupDeployment @parameters