如何创建Powershell脚本来创建Azure API管理资源

时间:2019-08-13 16:06:05

标签: azure powershell azure-api-management api-management apim

我试图弄清楚如何从脚本创建API管理资源和API管理API。我相信有两种选择:

  1. 使用AzureRm.ApiManagement中的Powershell管理api

    为此,我相信要使用的两个命令是:

    New-AzureRmApiManagement

    New-AzureRmApiManagementApi

  1. 使用Management REST API

    为此,我相信要创建资源,您将需要使用第一个选项,然后可以将以下方法与Invoke-RestMethod powershell命令一起使用:

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

我只想传递新的管理api资源的名称和devlab的名称以在其下创建并创建它,然后在其下创建一个与产品(或与无限制的产品相关联)的api默认产品...以较容易的为准。

任何人都可以帮助执行Powershell脚本吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

当您运行命令“ New-AzureRmApiManagementApi”时,您只需创建一个api,该api就不会添加到产品中。您需要运行命令“ Add-AzureRmApiManagementApiToProduct”将api添加到产品。我的脚本如下

$ResourceGroupName=""
$sku=""
$name="" 
$Location=""                   
$Organization=""
$AdminEmail=""
$apiName=" #the name of the web API."
$url="" #the URL of the web service that exposes the API.
$path="" #the web API path, which is the last part of the API's public URL and corresponds to the Web API URL suffix field in the admin portal.

#login azure
Connect-AzureRmAccount

#create api management instance with required parameters
New-AzureRmApiManagement -ResourceGroupName $ResourceGroupName -Name $name -Sku $sku -Location $Location -Organization $Organization -AdminEmail $AdminEmail

$ApiMgmtContext =New-AzureRmApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $name

#create api
$api = New-AzureRmApiManagementApi -Context $ApiMgmtContext -Name $apiName -ServiceUrl $url -Protocols @("http", "https") -Path $path

#run the command if you do not know product id
Get-AzureRmApiManagementProduct -Context $ApiMgmtContext

#add api to product
Add-AzApiManagementApiToProduct -Context $ApiMgmtContext -ProductId "" -ApiId $api.ApiId

有关Azure API管理powershell命令的更多详细信息,请参考document