如何正确调用Azure 资源管理器模板语言中定义的函数 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
命令管道位置 1 处的 cmdlet New-AzureResourceGroup
为以下参数提供值:
(输入 !? 寻求帮助。)
详细:对目标“psDeployment”执行“替换资源组...”操作。
详细:16:22:07 - 在位置“northeurope”创建资源组“psResourceGroup”
新 AzureResourceGroup:16:22:08 - 资源 Microsoft.Sql/servers/databases
'xxx-sql-server-name-xxx/psDatabaseName' 失败并显示消息
'无法处理资源的模板语言表达式
'/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'
在第 _ 行和第 _ 列。
'无法评估模板语言函数'resource Id':类型'Microsoft.Sql/servers/databases'需要'2'资源名称参数。''
在行:1 字符:1
+ 新 AzureResourceGroup -位置“北欧”-名称“psResourceGroup”-模板 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ New-AzureResourceGroup ],异常
+ FullyQualifiedErrorId:Microsoft.Azure.Commands.Resources.NewAzureResourceGroupCommand
根据文档,该函数resourceId()有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",
“参数”: {
“地点”: {
“类型”:“字符串”,
"defaultValue": "北欧",
“允许值”:[
“东亚”,
“东南亚”,
“美国东部”,
“美国西部”,
“美国中北部”,
“美国中南部”,
“美国中部”,
“北欧”,
《西欧》
]
},
"sqlServerName": { "type": "string" },
"sqlAdminUserName": { "type": "string" },
"sqlAdminUserPassword": { "type": "securestring" },
“数据库名称”:{“类型”:“字符串”}
},
“资源”: [
{
"type": "Microsoft.Sql/servers",
"apiVersion": "2.0",
“位置”:“[参数('位置')]”,
“名称”:“[参数('sqlServerName')]”,
“属性”:{“管理员登录”:“[参数('sqlAdminUserName')]”,“管理员登录密码”:“[参数('sqlAdminUserPassword')]”},
“资源”: [
{
“类型”:“数据库”,
"apiVersion": "2.0",
“位置”:“[参数('位置')]”,
“名称”:“[参数('数据库名称')]”,
“dependsOn”:[“[concat('Microsoft.Sql/servers/',参数('sqlServerName'))]”],
“特性”: {
“版本”:“标准”,
"排序规则": "SQL_Latin1_General_CP1_CI_AS",
"maxSizeBytes": "10737418240",
"requestedServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b",
"createMode": "复制",
====> "sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"
}
}
]
}
]
}