如何使用Powershell在Azure API管理中禁用订阅密钥

如何使用Powershell在Azure API管理中禁用订阅密钥,azure,powershell,azure-api-management,Azure,Powershell,Azure Api Management,我们正在使用Azure API管理和Powershell通过脚本导入门户上的配置,但是,每当导入API时,就会启用来自门户的“需要订阅”检查(需要API的订阅密钥)。我们现在还没有在API上使用订阅功能,所以我们需要在导入时禁用它。我们正在使用以下代码使用Import AzApiManagementApi和Set AzApiManagementApi: Set AzApiManagementApi-apid$apid-Context$Context-Protocols@('https')-Ser

我们正在使用Azure API管理和Powershell通过脚本导入门户上的配置,但是,每当导入API时,就会启用来自门户的“需要订阅”检查(需要API的订阅密钥)。我们现在还没有在API上使用订阅功能,所以我们需要在导入时禁用它。我们正在使用以下代码使用
Import AzApiManagementApi
Set AzApiManagementApi

Set AzApiManagementApi-apid$apid-Context$Context-Protocols@('https')-ServiceUrl$serviceBase$path-Name$api.Name
设置AzApiManagementPolicy-Context$Context-apid$apid-PolicyFilePath“$pwd/src/private/security_policy.xml”

我们没有找到在没有此检查的情况下导入API的方法。是否有任何脚本可通过powershell禁用此功能?

您可以使用Arm模块命令集AzureRmApiManagementProduct或Az模块命令集AzApiManagementProduct来禁用所需的订阅选项

检查以下文件

更新

我可以看到set azapimanagementapi现在支持SubscriptionRequired参数

设置AzApiManagementApi
-上下文
-蜂鸟
[-姓名]
[-说明]
[-ServiceUrl]
[-Path]
[-协议]
[-AuthorizationServerId]
[-授权范围]
[-OpenIdProviderId]
[-BealerTokenSendingMethod]
[-SubscriptionKeyHeaderName]
[-SubscriptionKeyQueryParamName]
[-需要订阅]
[-PassThru]
[-DefaultProfile]
[]

快速添加到该文件中。以下Powershell命令将API的Subscription Required属性设置为false。不过要提醒一句:这目前不适用于Azure DevOps管道。管道将声明任务已成功运行,如果在管道中运行$api的回显,则将显示api已SubscriptionRequired:False。但是,如果您在Azure中检查api,它仍将设置为True(默认行为)


我无法按照建议使用Set-AzApiManagementApi将导入的API上的SubscriptionRequired更改为false。由于SubscriptionRequired是一个开关参数,我无法将其指定为false

然而,一种不同的方法奏效了@RomericRobo的答案缺少保存更改的最终设置AzApiManagementApi命令(抱歉,我无法编辑或评论他的答案)。我的完整工作解决方案是:

# get context
$apimContext = New-AzApiManagementContext -ResourceGroupName "targetResourceGroup" -ServiceName "targetApimInstance"
$api = Get-AzApiManagementApi -Context $apimContext -ApiId "api-id"

# set subscriptionRequired to false
$api.SubscriptionRequired=$false
Set-AzApiManagementApi -InputObject $api

此问答的潜在副本--我当前没有使用订阅,您的意思是我应该创建一个没有关键要求的订阅,并将其指定给我要导入的api?我确实测试了它,从产品中删除了配置,然后将api与此产品一起导入,默认情况下,检查仍然为true。您需要使用回答中提到的命令将subscription requires属性显式设置为false。您是对的,最终这样做了,但是感觉像是黑客,因为API上的检查仍然为true。添加产品的命令已记录在案,我想您已经将其附加到产品上了,很抱歉造成混淆。你是对的,实际上我没有找到一种方法来创建带有禁用必需订阅选项的api,希望他们将来会添加它。下面是AzApiManagement命令的完整列表
# get context
$apimContext = New-AzApiManagementContext -ResourceGroupName "targetResourceGroup" -ServiceName "targetApimInstance"
$api = Get-AzApiManagementApi -Context $apimContext -Name "Name of Api"

# set subscriptionRequired to false
$api.SubscriptionRequired=$false
# get context
$apimContext = New-AzApiManagementContext -ResourceGroupName "targetResourceGroup" -ServiceName "targetApimInstance"
$api = Get-AzApiManagementApi -Context $apimContext -ApiId "api-id"

# set subscriptionRequired to false
$api.SubscriptionRequired=$false
Set-AzApiManagementApi -InputObject $api