Instantly share code, notes, and snippets.

What would you like to do?
# Creates Event Grid Subscription.
# Creates an Event Grid Subscription (Topic type: Azure Subscription) to a Function App V2
# Progress messages
Function Get-AppServicePublishingCredentials($resourceGroupName, $functionAppName){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$functionAppName/publishingcredentials"
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
return $publishingCredentials
#function to get bearer token from publishing profile
function Get-KuduAccessToken($resourceGroupName, $functionAppName){
$publishingCredentials = Get-AppServicePublishingCredentials $resourceGroupName $functionAppName
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
#function to get the Master Key using End point and passing bearer tocken in Authorization Header
function Get-MasterAPIKey($accessToken, $functionAppName ){
$result = Invoke-RestMethod -Uri "https://$" -Headers @{"Authorization"=$accessToken;"If-Match"="*"}
return $result
function Get-EventGridExtensionSystemKey($functionAppName, $masterKey ){
$result = Invoke-RestMethod ("https://$" + $masterKey.masterKey.ToString())
return $result.value
function Create-EventGridSubscription($functionAppName, $functionName, $systemKey, $eventGridSubscriptionName)
$functionEndpointURL = "https://" + $functionAppName + "" + $functionName + "&code=" + $systemkey +""
New-AzureRmDeployment `
-Name egdeployment `
-Location westeurope `
-TemplateUri `
-eventSubName $eventGridSubscriptionName `
-endpoint $functionEndpointURL
# Get Kudu Access Token, so we can call Kudu API to get master key
$accessToken = Get-KuduAccessToken $ResourceGroupName $FunctionAppName
Write-Output "This is the accesstoken: $accessToken"
# Get master key to call Event Grid Extension API
$masterKey = Get-MasterAPIKey $accessToken $FunctionAppName
Write-Output "This is the masterKey: $masterkey"
# Get Event Grid SystemKey, so we can create the Event Grid Webhook
$systemKey = Get-EventGridExtensionSystemKey $FunctionAppName $masterkey
Write-Output "This is the SystemKey: $systemKey"
Write-Output "Creating the Event Grid Subscription:"
Create-EventGridSubscription $FunctionAppName $FunctionName $systemKey $EventGridSubscriptionName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment