使用 powershell 将带有模板参数的操作添加到 azure api

Using powershell to add an operation with template parameters to an azure api

尝试向 Azure Api 管理 Api 添加操作时,使用 powershell cmdlt New-AzureRmApiManagementOperation,命令 returns 无信息响应:ValidationError: One or more fields contain incorrect values.

当操作的 UrlTemplate 不包含任何参数(即 curly 大括号中的 url 片段)时,该命令成功 运行,但不处理其中的调用他们在场。

这按预期工作:

New-AzureRmApiManagementOperation -Context $context -ApiId $aid -OperationId $oid -Name $name -Method $method -UrlTemplate '/all'

但事实并非如此:

New-AzureRmApiManagementOperation -Context $context -ApiId $aid -OperationId $oid -Name $name -Method $method -UrlTemplate '/{id}'

似乎提供的信息 in the documentation 有点不准确。当存在参数时,TemplateParameters 不是可选的,并且不会自动生成。

必须存在 Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementParameter 类型的对象数组,并且对于 url 模板中的每个参数,它必须包含具有与参数相同值的 Name 的元素值,并且 Type 是某个枚举的成员,大概匹配一些 swagger 定义。

以下代码从模板字符串生成这样的数组:

$tparam = Select-String "{([^}]+)}" -input $template -AllMatches |
    Foreach { $_.matches } |
    Foreach {
        $p = New-Object -TypeName Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementParameter
        $p.Name = $_.Groups[1].Value
        $p.Type = "string"
        return $p
    }

后面可以是:

New-AzureRmApiManagementOperation -Context $context -ApiId $aid -OperationId $oid -Name $name -Method $method -UrlTemplate $template -TemplateParameters $tparam