json - Azure 资源管理器模板语言-resourceId() : Unable to evaluate template language function 'resource Id'

标签 json powershell azure azure-management-api azure-resource-manager

如何正确调用Azure Resource Manager Template Language中定义的函数resourceId() ?

上下文

///  - 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



根据documentation,函数resourceId()具有, 2 个参数,我用两个我认为正确的常量字符串来调用它:
resourceId('Microsoft.Sql/servers/databases', 'TestDB')
它仍然会产生一条错误消息,指示参数数量错误:
“无法评估模板语言函数“资源 Id”:类型“Microsoft.Sql/servers/databases”需要“2”个资源名称参数。”

根据错误消息,使用的资源是: '/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')]"

                    }
                }
            ]
        }
    ]
}

最佳答案

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

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

关于json - Azure 资源管理器模板语言-resourceId() : Unable to evaluate template language function 'resource Id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29779642/

相关文章:

java - 如何从 JSON 字符串获取数据?

python - 如何序列化/反序列化这个实体?

Powershell 比较 if 结构中的字符串给出错误结果

python - 如何在 Windows 上执行 .sh 文件? (学习Python)

sql-server - 寻求有关将现有 asp.net/sql 服务器网站迁移到 Azure VM 的建议

json - 如何将字符串从 JSON 转换为 Swift 中的对象数组?

javascript - Angular session 存储不起作用

git - 如何在现有仓库中创建子模块

运行 "az aks get-credentials"命令时 Azure Kubernetes 错误

Azure 流量管理器通过不同数据中心的嵌套配置文件实现性能平衡