首页 > 解决方案 > 用于提取所有 API、它们的操作和入站策略的 Powershell 脚本

问题描述

我正在寻找一个 PowerShell 脚本来提取所有 API、它们的操作和入站策略

我尝试过使用下面的 Microsoft 文章,但无法征服。

https://docs.microsoft.com/en-us/powershell/module/az.apimanagement/export-azapimanagementapi?view=azps-5.4.0 https://docs.microsoft.com/en-us/powershell/module /az.apimanagement/get-azapimanagementoperation?view=azps-5.4.0 https://docs.microsoft.com/en-us/powershell/module/azurerm.apimanagement/export-azurermapimanagementapi?view=azurermps-6.13.0

Expected output is 

API M (Folder) 
API-1 (Subfolder-1)
  A.Operation-1 (subfolder-A)
    inbound Policy.xml
  B.Operation-2 (subfolder-B)
    inbound Policy.xml

API-2 (SubFolder-2)
  A.Operation-1 (subfolder-A)
    inbound Policy.xml
  B.Operation-2 (subfolder-B)
    inbound Policy.xml

标签: azureazure-powershellazure-api-managementazure-automation

解决方案


如果您想通过 API Path 将所有 API 操作入站策略作为 .xml 文件导出到某些文件夹,请尝试以下代码:

$apimName = "<your apim name>"
$apimSresourceGroup = "<your apim resource group>"
$exportPath = "d:/APIM/"
mkdir $exportPath 

$apim_context = New-AzApiManagementContext -ResourceGroupName $apimSresourceGroup -ServiceName $apimName
$APIs = Get-AzApiManagementApi -Context $apim_context 
foreach($API in $APIs){
    $APIPath = $exportPath + $API.Name + '/'
    mkdir $APIPath
    $allOpers = Get-AzApiManagementOperation  -Context $apim_context -ApiId $API.ApiId
    foreach($oper in $allOpers){
        $operPath  =  $APIPath +$oper.Method + '-' + $oper.Name
        mkdir $operPath
        $policy =[xml]@(Get-AzApiManagementPolicy -Context $apim_context -ApiId $oper.ApiId -OperationId $oper.OperationId)
        $inBoundPolicyContent = $policy.policies.inbound.OuterXml
        New-Item -Path $operPath -Name 'inbound.xml' -value $inBoundPolicyContent 
    }
}

结果

所有 API 文件夹: 在此处输入图像描述

特定API的所有操作: 在此处输入图像描述

某些操作的入站策略: 在此处输入图像描述

更新:

如果您还想获取 API 级别的入站策略,请尝试以下代码:

$apimName = "<your apim name>"
$apimSresourceGroup = "<your apim resource group>"
$exportPath = "d:/APIM/"
mkdir $exportPath 

$apim_context = New-AzApiManagementContext -ResourceGroupName $apimSresourceGroup -ServiceName $apimName
$APIs = Get-AzApiManagementApi -Context $apim_context 
$APIs = Get-AzApiManagementApi -Context $apim_context 
foreach($API in $APIs){
    $APIPath = $exportPath + $API.Name + '/'
    mkdir $APIPath
    #save API level inbound policy 
    $API_policy =[xml]@(Get-AzApiManagementPolicy -Context $apim_context -ApiId $API.ApiId)
    $inBoundPolicyContent = $API_policy.policies.inbound.OuterXml
    New-Item -Path $APIPath -Name 'inbound.xml' -value $inBoundPolicyContent 
    #save API level inbound policy end
    $allOpers = Get-AzApiManagementOperation  -Context $apim_context -ApiId $API.ApiId
    foreach($oper in $allOpers){
        $operPath  =  $APIPath +$oper.Method + '-' + $oper.Name
        mkdir $operPath
        $policy =[xml]@(Get-AzApiManagementPolicy -Context $apim_context -ApiId $oper.ApiId -OperationId $oper.OperationId)
        $inBoundPolicyContent = $policy.policies.inbound.OuterXml
        New-Item -Path $operPath -Name 'inbound.xml' -value $inBoundPolicyContent 
    }
}

只看下面的代码save API level inbound policy

结果:

在此处输入图像描述 在此处输入图像描述

如果您还有其他问题,请告诉我。


推荐阅读