2

I am using KeyVault for storing Ocp-Apim-Subscription-Key. Applications are getting this key from KeyVault and making requests to API Management successfully.

But now, i want to rotate this Ocp-Apim-Subscription-Key frequently using powershell scripts in Runbooks. I can't find a way to regerenate Ocp-Apim-Subscription-Key through powershell. Is this possible ? If there is another option to rotate this key then please let me know.

Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90

2 Answers2

2
# Get API Management Services information and set context
$ApiManagements = Get-AzApiManagement
foreach ($ApiManagement in $ApiManagements)
{
  $ApiManagementContext = New-AzApiManagementContext -ResourceId $ApiManagement.Id

  # Get all API Management Subscriptions
  $ApiManagementSubscriptions = Get-AzApiManagementSubscription -Context $ApiManagementContext
  foreach ($ApiManagementSubscription in $ApiManagementSubscriptions)
  {
    # Update the Keys
    $PrimaryKey = (New-Guid) -replace '-',''
    $SecondaryKey = (New-Guid) -replace '-',''
    Set-AzApiManagementSubscription -Context $ApiManagementContext -SubscriptionId $ApiManagementSubscription.SubscriptionId -PrimaryKey $PrimaryKey `
                                    -SecondaryKey $SecondaryKey -State Active 
  }
}  
Felix Bodmer
  • 291
  • 2
  • 16