Azure资源管理器模板语言 - resourceId():无法评估模板语言函数“资源ID”Azure资源管理器模板语言

问题描述 投票:3回答:3

How do you correctly call the function resourceId() defined in the Azure Resource Manager Template Language?

上下文

///  - Azure
///  - Azure Resource Management
///      https://msdn.microsoft.com/en-us/library/azure/dn578292.aspx
///
///  - Azure Resource Manager Template Language
///      https://msdn.microsoft.com/en-us/library/azure/dn835138.aspx
///
///  - Azure Resource Manager Template Language functions and expressions
///  - Azure Resource Manager Template Language function:
///      resourceId('resourceNamespace/resourceType','resourceName')
///
///  - Powershell
///  - Azure PowerShell
///  - Azure PowerShell Resource Manager Mode (Switch-AzureMode AzureResourceManager)
///  - Azure PowerShell CmdLet: New-AzureResourceGroup
///

模板中的这一行(参见下面的完整模板) "sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]" 运行PowerShell New-AzureResourceGroup CmdLet时出现此错误:

    PS c:\AzureDeployment> New-AzureResourceGroup -Location "North Europe" -Name "psResourceGroup" -DeploymentName "psDeployment" -TemplateFile .\Template.json -TemplateParameterFile .\Parameters.json -Verbose
    cmdlet New-AzureResourceGroup at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    VERBOSE: Performing the operation "Replacing resource group ..." on target "psDeployment".
    VERBOSE: 16:22:07 - Created resource group 'psResourceGroup' in location 'northeurope'
    New-AzureResourceGroup : 16:22:08 - Resource Microsoft.Sql/servers/databases 
    'xxx-sql-server-name-xxx/psDatabaseName' failed with message 
    'Unable to process template language expressions for resource
    '/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'
    at line _ and column _. 
    'Unable to evaluate template language function 'resource Id': the type 'Microsoft.Sql/servers/databases' requires '2' resource name argument(s).''
    At line:1 char:1
    + New-AzureResourceGroup -Location "North Europe" -Name "psResourceGroup" -Templat ... 
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : NotSpecified: (:) [ New-AzureResourceGroup ], Exception 
        + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureResourceGroupCommand

根据resourceId(),函数documentation有2个参数,我用两个常量字符串称它是正确的: resourceId('Microsoft.Sql/servers/databases', 'TestDB') 仍然会产生一条错误消息,指出参数的数量是错误的: 'Unable to evaluate template language function 'resource Id': the type 'Microsoft.Sql/servers/databases' requires '2' resource name argument(s).'

根据错误消息使用的资源是:'/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'

那么,为数据库调用resourceId()的正确方法是什么?

此外,如果我从模板中删除createMode和sourceDatabaseId一切正常。

这是上面使用的模板

{    
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",

    "parameters": {
        "location": {
            "type": "string",
            "defaultValue": "North Europe",
            "allowedValues": [
                "East Asia",
                "South East Asia",
                "East US",
                "West US",
                "North Central US",
                "South Central US",
                "Central US",
                "North Europe",
                "West Europe"
            ]
        },
        "sqlServerName": { "type": "string" },
        "sqlAdminUserName": { "type": "string" },
        "sqlAdminUserPassword": { "type": "securestring" },
        "databaseName": { "type": "string" }
    },

    "resources": [
        {
            "type": "Microsoft.Sql/servers",
            "apiVersion": "2.0",
            "location": "[parameters('location')]",
            "name": "[parameters('sqlServerName')]",
            "properties": { "administratorLogin": "[parameters('sqlAdminUserName')]", "administratorLoginPassword": "[parameters('sqlAdminUserPassword')]" },
            "resources": [
                {
                    "type": "databases",
                    "apiVersion": "2.0",
                    "location": "[parameters('location')]",
                    "name": "[parameters('databaseName')]",
                    "dependsOn": [ "[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]" ],
                    "properties": {
                        "edition": "Standard",
                        "collation": "SQL_Latin1_General_CP1_CI_AS",
                        "maxSizeBytes": "10737418240",
                        "requestedServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b",
                        "createMode": "Copy",

====>                   "sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"

                    }
                }
            ]
        }
    ]
}

json powershell azure azure-management-api azure-resource-manager
3个回答
7
投票

我在一篇完全不相关的文章中偶然发现了解决方案,但你应该过去了

[resourceId('Microsoft.SQL/servers/databases', parameters('sqlServerName'), 'TestDB')]


0
投票

我注意到你的apiVersion值格式不正确。 Azure使用API​​的日期值。

尝试使用示例文件中的值:

"apiVersion": "2014-04-01-preview"

0
投票

因为你丢失了SQL服务器的名字?

© www.soinside.com 2019 - 2024. All rights reserved.