Skip to content

Instantly share code, notes, and snippets.

@GBSmulders
Created November 7, 2018 11:16
Show Gist options
  • Save GBSmulders/6cf61e853618312b484c2173dbcb5d7a to your computer and use it in GitHub Desktop.
Save GBSmulders/6cf61e853618312b484c2173dbcb5d7a to your computer and use it in GitHub Desktop.
This script sets the given key and value as Host Key in an Azure Function App
<#
.SYNOPSIS
Sets a host key of an Azure Function App to a given value
.DESCRIPTION
This script sets the given key and value as Host Key in an Azure Function App
.PARAMETER appName
The name of the Azure Function App
.PARAMETER resourceGroup
The name of the resourcegroup in which this the Azure Function App is placed
.PARAMETER keyName
The name of the key to insert
.PARAMETER keyValue
The name of the key to insert
.PARAMETER principalUser
Service pricipal user used to login
.PARAMETER principalPassword
Password for the service principal
.PARAMETER principalTenant
Tenant of the service principal
*Hint: Run the following command in the Azure CLI to retrieve the tennant:
az account show --query 'tenantId'
#>
param([string]$appName,
[string]$resourceGroup,
[string]$keyName,
[string]$keyValue,
[string]$principalUser,
[string]$principalPassword,
[string]$principalTenant
)
# Login in with Service Principal $servicePrincipal
az login --service-principal -u $principalUser -p $principalPassword -t $principalTenant
# Get Username and password for Kudu
$user = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup --query "[?publishMethod=='MSDeploy'].userName" --o tsv
$pass = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup --query "[?publishMethod=='MSDeploy'].userPWD" --o tsv
$pair = "$($user):$($pass)"
$kuduCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
# Get token from Kudu
$token = Invoke-RestMethod -Uri "https://$appName.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization = ("Basic {0}" -f $kuduCreds)} -Method GET
# Prepare call
$body = @{
name = $keyName
value = $keyValue
} | ConvertTo-Json
$uri = "https://$appName.azurewebsites.net/admin/host/keys/$keyName"
# Call API
Invoke-RestMethod -Method Put -Headers @{Authorization = ("Bearer {0}" -f $token); Accept = "application/json"} -ContentType "application/json" -Uri $uri -Body $body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment