从 powershell 更新 azure api 入站策略

问题描述

首先感谢您对此进行调查。

我需要从 Powershell 更新由 Azuremanagementservice 管理的 API 的入站策略。为此,我尝试从 powershell 访问 API 不幸的是最终没有任何进展。

我正在通过 powershell 寻找什么 a) AzuremanagementService下的访问API b) 更新入站操作策略

谢谢

解决方法

如果您想通过 Azure PowerShell 设置策略,请尝试以下代码(为演示添加根级别的入站 IP 阻止策略):

$newPolicy = '<policies>
                <inbound>
                    <ip-filter action="forbid">
                        <address-range from="192.168.0.1" to="192.168.0.2" />
                    </ip-filter>
                </inbound>
                <backend>
                    <forward-request />
                </backend>
                <outbound />
                <on-error />
             </policies>'

$apim_context = New-AzApiManagementContext -ResourceGroupName "<resource group name>" -ServiceName "<API management service name>"

Set-AzApiManagementPolicy -Context $apim_context  -Policy $newPolicy

结果: enter image description here

有关 Set-AzApiManagementPolicy 操作的更多信息,请参阅 this reference doc

更新

如果要在 API 级别修改策略,则需要使用以下命令获取所有 ApiId

Get-AzApiManagementApi -Context $apim_context | Select-Object Name,ApiId

enter image description here

我将 ApiID 指定为“echo-api”:

Set-AzApiManagementPolicy -Context $apim_context  -Policy $newPolicy -ApiId 'echo-api'

结果: enter image description here

有关使用 PowerShell 设置策略的更多信息,请参阅 here

,

您可以使用 REST API 执行此操作:
Api Operation Policy - Create Or Update

PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/operations/{operationId}/policies/policy?api-version=2019-12-01