我正在寻找一个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
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发布于 2021-02-01 03:03:21
如果要将所有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下面的代码即可。
结果:


如果你还有什么问题请告诉我。
https://stackoverflow.com/questions/65948588
复制相似问题