0

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://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
SKumar
  • 33
  • 9
  • How's going? Has your issue got solved? – Stanley Gong Feb 15 '21 at 01:28
  • Thanks for proving the solution however I am not able to get inbound policy for "all operations" Refer to below screenshot for the need here # https://i.stack.imgur.com/Voho5.png – SKumar Feb 15 '21 at 02:33

1 Answers1

0

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.

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16
  • Thanks for proving the solution however I am not able to get inbound policy for "all operations" Refer to below screenshot https://i.stack.imgur.com/Voho5.png – SKumar Feb 06 '21 at 00:42
  • @SKumar, I have updated my solution, could you please accept it if it helps you? – Stanley Gong Feb 16 '21 at 04:09