Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

azure - Powershell script to extract all APIs, their operations and inbound policies

I am looking for a PowerShell script to extract all APIs, their operations and inbound policies

I have tried approaching with below MicroSoft articles but could not conquer.

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
question from:https://stackoverflow.com/questions/65948588/powershell-script-to-extract-all-apis-their-operations-and-inbound-policies

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to export all API operation inbound policy as .xml file to some folders by API Path just try the code below:

$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 
    }
}

Result

All API folders: enter image description here

All operations of certain API: enter image description here

Inbound policy of certain operation: enter image description here

UPDATE:

If you also want to get API level inbound policy, try the code below:

$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 
    }
}

Just see the code under save API level inbound policy.

Result:

enter image description here enter image description here

Let me know if you have any further questions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...