上下文
/// - 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')
它仍然会产生一条错误信息,表明参数的数量是错误的:
'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')]" } } ] } ] }
答案 0 :(得分:6)
我在一篇完全不相关的文章中偶然发现了解决方案,但你应该通过
[resourceId('Microsoft.SQL/servers/databases', parameters('sqlServerName'), 'TestDB')]
答案 1 :(得分:0)
我注意到您的apiVersion值格式不正确。 Azure使用API的日期值。
尝试使用示例文件中的值:
"apiVersion": "2014-04-01-preview"
答案 2 :(得分:0)
因为您丢失了SQL Server的名称?