Skip to content

Instantly share code, notes, and snippets.

@RichieBzzzt
Last active July 12, 2018 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichieBzzzt/75f81be2e44463fe95b7f88c7d584506 to your computer and use it in GitHub Desktop.
Save RichieBzzzt/75f81be2e44463fe95b7f88c7d584506 to your computer and use it in GitHub Desktop.
param(
[parameter(Mandatory=$true)] [string]$functionName,
[parameter(Mandatory=$true)] [string]$appName,
[parameter(Mandatory=$true)] [string]$resourceGroup,
[parameter(Mandatory=$true)] [string]$key
)
function Get-KuduCredentials($appName, $resourceGroup) {
[xml]$x = Get-AzureRmWebAppPublishingProfile -ResourceGroupName $resourceGroup -Name $appName
$user = $x.publishData.publishProfile[1].userName.Split("\") | Select-Object -Last 1
$pass = $x.publishData.publishProfile[1].userPWD
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
return $encodedCreds
}
function Get-FunctionKey([string]$appName, [string]$functionName, [string]$encodedCreds) {
$jwt = Invoke-RestMethod -Uri "https://$appName.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization = ("Basic {0}" -f $encodedCreds)} -Method GET
$keys = Invoke-RestMethod -Method GET -Headers @{Authorization = ("Bearer {0}" -f $jwt)} `
-Uri "https://$appName.azurewebsites.net/admin/functions/$functionName/keys"
# -Uri "https://$appName.azurewebsites.net/admin/functions/$functionName/keys/default"
$code = $keys.keys[0].value
return $code
}
function Set-FunctionKey([string]$appName, [string]$functionName,[string]$keyValue, [string]$encodedCreds) {
$jwt = Invoke-RestMethod -Uri "https://$appName.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization = ("Basic {0}" -f $encodedCreds)} -Method GET
$body = "{
""name"": ""adf"",
""value"" : ""$($keyvalue)""
}"
$keys = Invoke-RestMethod -Method PUT -Headers @{Authorization = ("Bearer {0}" -f $jwt)} `
-Uri "https://$appName.azurewebsites.net/admin/functions/$functionName/keys/adf" `
-Body $body `
-ContentType 'application/json'
return $keys
}
if ([string]::IsNullOrEmpty($(Get-AzureRmContext).Account)) {
Login-AzureRmAccount
}
$kuduCreds = Get-KuduCredentials $appName $resourceGroup
Set-FunctionKey $appName $functionName $key $kuduCreds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment