azure - 用于提取所有 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
解决方案
如果您想通过 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 级别的入站策略,请尝试以下代码:
$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
。
结果:
如果您还有其他问题,请告诉我。
推荐阅读
- python - Anaconda python 优先于系统 python
- python - Qt视图成对垂直显示数据列
- python - 比较两个 JSON 结果并生成新条目的新 JSON (Python)
- javascript - 无法在 javascript 标签中使用 html 代码
- c# - 有没有办法通过反射获取 ASP.NET Core Razor Pages 中所有 PageModel 类的 url?
- android - 使用 Espresso 对 Fragment、视图模型、DataStore Preferences 进行端到端测试
- angular - 在Angular中的另一个页面中锚定滚动
- python - 在 x0 中计算的同一函数的多个导数
- php - 使用阿拉伯语导入 Calibri 字体不起作用
- solidity - 如何在 Solidity 中编写国际象棋游戏?