首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提取所有API、它们的操作和入站策略的Powershell脚本

提取所有API、它们的操作和入站策略的Powershell脚本
EN

Stack Overflow用户
提问于 2021-01-29 04:00:12
回答 1查看 1.1K关注 0票数 0

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

我试图接近下面的MicroSoft文章,但无法征服。

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-02-01 03:03:21

如果要将所有API操作入站策略按.xml路径导出为某些文件夹,只需尝试下面的代码:

代码语言:javascript
复制
$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级别的入站策略,请尝试下面的代码:

代码语言:javascript
复制
$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下面的代码即可。

结果:

如果你还有什么问题请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65948588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档