Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Param(
- [string] [Parameter(Mandatory=$true)] $ResourceGroup,
- [string] [Parameter(Mandatory=$true)] $AppName,
- [string] [Parameter(Mandatory=$true)] $FunctionName,
- [string] [Parameter(Mandatory=$true)] $KeyName,
- [string] [Parameter(Mandatory=$true)] $KeyValue
- )
- function getAuthenticationToken([string]$appName, [string]$resourceGroup)
- {
- $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)"
- $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
- $jwt = Invoke-RestMethod -Uri "https://$appName.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $encodedCreds)} -Method GET
- return $jwt
- }
- function setFunctionKey([string]$appName, [string]$functionName, [string] $keyName, [string]$keyValue, [string]$jwt)
- {
- $body = (@{
- "name" = $keyName
- "value" = $keyValue
- } | ConvertTo-Json)
- #Setting the SecurityProtocol is a workaround for calling Azure APIs, I think?
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
- try {
- Invoke-RestMethod -Uri "https://$appName.azurewebsites.net/admin/functions/$functionName/keys/$keyName/" `
- -Headers @{Authorization=("Bearer $jwt")} `
- -Method PUT `
- -ContentType "application/json" `
- -Body $body
- } catch {
- $_.Exception | Format-List -Force
- }
- }
- $jwt = getAuthenticationToken $AppName $ResourceGroup
- setFunctionKey $AppName $FunctionName $KeyName $KeyValue $jwt
- Write-Host "Specified key '$KeyName' has been added to $FunctionName"
- $user = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
- --query "[?publishMethod=='MSDeploy'].userName" -o tsv
Add Comment
Please, Sign In to add comment