You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell添加path模式v1版本的Azure APIM API?求非门户替代方案

Hey there! Let's break down your Azure API Management questions with practical, portal-free solutions:

1. How to add an API with "path" versioning mode and version identifier "v1" via PowerShell?

The modern Azure PowerShell Az module has full support for path-based versioning. Here's a step-by-step breakdown:

  1. First, get your APIM instance context:

    $apimContext = Get-AzApiManagement -ResourceGroupName "your-resource-group-name" -Name "your-apim-instance-name"
    
  2. Create an API version set configured for path mode:

    $versionSet = New-AzApiManagementApiVersionSet -Context $apimContext `
        -Name "v1-path-version-set" `
        -Scheme "Path" `
        -Description "API version set using path-based versioning"
    
  3. Create your API and link it to the version set, specifying the "v1" identifier as the path segment:

    New-AzApiManagementApi -Context $apimContext `
        -Name "YourTargetAPI" `
        -ServiceUrl "https://your-backend-service-url.com" `
        -Path "v1" `
        -ApiVersionSetId $versionSet.Id `
        -ApiVersion "v1" `
        -Description "API with path-based version v1"
    

This will create an API where the version is accessed via the /v1 path prefix.

2. Alternative implementation without using the portal since Import-AzureRmApiManagementApi doesn't support "path" version mode?

You're right that the older Import-AzureRmApiManagementApi cmdlet lacks support for path-based versioning. Here are three solid portal-free alternatives:

Option 1: Switch to the Az module's Import-AzApiManagementApi

The updated Az module cmdlet supports versioning parameters. Follow these steps:

  1. Create the path-based version set (same as step 2 in the first question).
  2. Import your API specification while linking it to the version set:
    Import-AzApiManagementApi -Context $apimContext `
        -SpecificationFormat "OpenApi" `
        -SpecificationPath "C:\path\to\your-api-spec.yaml" `
        -ApiVersionSetId $versionSet.Id `
        -ApiVersion "v1" `
        -Path "v1"
    

Option 2: Use Azure REST API with PowerShell (if stuck on AzureRm)

If you can't switch to Az, you can directly call the Azure Management REST API using Invoke-RestMethod:

  1. Get an access token for authentication:

    $token = (Get-AzAccessToken -ResourceUrl "https://management.azure.com").Token
    $headers = @{
        "Authorization" = "Bearer $token"
        "Content-Type" = "application/json"
    }
    
  2. Create the path-based version set:

    $versionSetPayload = @{
        properties = @{
            displayName = "v1-path-version-set"
            versioningScheme = "Path"
            description = "Path-based version set for API import"
        }
    } | ConvertTo-Json
    
    $versionSetResponse = Invoke-RestMethod -Method Post `
        -Uri "https://management.azure.com/subscriptions/{your-subscription-id}/resourceGroups/{your-resource-group}/providers/Microsoft.ApiManagement/service/{your-apim-instance}/apiVersionSets?api-version=2021-08-01" `
        -Headers $headers `
        -Body $versionSetPayload
    
    $versionSetId = $versionSetResponse.id
    
  3. Import your API with the version set linked:

    $apiSpecContent = Get-Content "C:\path\to\your-api-spec.json" -Raw
    $importPayload = @{
        properties = @{
            serviceUrl = "https://your-backend-service-url.com"
            path = "v1"
            apiVersion = "v1"
            apiVersionSetId = $versionSetId
            contentFormat = "openapi+json"
            contentValue = $apiSpecContent
        }
    } | ConvertTo-Json
    
    Invoke-RestMethod -Method Put `
        -Uri "https://management.azure.com/subscriptions/{your-subscription-id}/resourceGroups/{your-resource-group}/providers/Microsoft.ApiManagement/service/{your-apim-instance}/apis/YourTargetAPI?api-version=2021-08-01" `
        -Headers $headers `
        -Body $importPayload
    

    Don't forget to replace all placeholder values (like subscription ID, resource group name) with your actual data.

Option 3: Use Azure CLI

If you're open to using CLI instead of PowerShell, this is a straightforward approach:

  1. Create the path-based version set:

    az apim api-version-set create --resource-group your-resource-group `
        --service-name your-apim-instance `
        --name v1-path-version-set `
        --scheme Path `
        --description "Path-based versioning for imported API"
    
  2. Import your API and link it to the version set:

    az apim api import --resource-group your-resource-group `
        --service-name your-apim-instance `
        --name YourTargetAPI `
        --specification-path ./your-api-spec.yaml `
        --specification-format OpenApi `
        --path v1 `
        --api-version v1 `
        --api-version-set-id $(az apim api-version-set show --resource-group your-resource-group --service-name your-apim-instance --name v1-path-version-set --query id -o tsv)
    

内容的提问来源于stack exchange,提问作者Janusz Nowak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:47:12