Guest User

Untitled

a guest
Aug 22nd, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. Param(
  2. [string] [Parameter(Mandatory=$true)] $ResourceGroup,
  3. [string] [Parameter(Mandatory=$true)] $AppName,
  4. [string] [Parameter(Mandatory=$true)] $FunctionName,
  5. [string] [Parameter(Mandatory=$true)] $KeyName,
  6. [string] [Parameter(Mandatory=$true)] $KeyValue
  7. )
  8.  
  9. function getAuthenticationToken([string]$appName, [string]$resourceGroup)
  10. {
  11. $user = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
  12. --query "[?publishMethod=='MSDeploy'].userName" -o tsv
  13.  
  14. $pass = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
  15. --query "[?publishMethod=='MSDeploy'].userPWD" -o tsv
  16.  
  17. $pair = "$($user):$($pass)"
  18. $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
  19.  
  20. $jwt = Invoke-RestMethod -Uri "https://$appName.scm.azurewebsites.net/api/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $encodedCreds)} -Method GET
  21.  
  22. return $jwt
  23. }
  24.  
  25. function setFunctionKey([string]$appName, [string]$functionName, [string] $keyName, [string]$keyValue, [string]$jwt)
  26. {
  27. $body = (@{
  28. "name" = $keyName
  29. "value" = $keyValue
  30. } | ConvertTo-Json)
  31.  
  32. #Setting the SecurityProtocol is a workaround for calling Azure APIs, I think?
  33. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  34. try {
  35. Invoke-RestMethod -Uri "https://$appName.azurewebsites.net/admin/functions/$functionName/keys/$keyName/" `
  36. -Headers @{Authorization=("Bearer $jwt")} `
  37. -Method PUT `
  38. -ContentType "application/json" `
  39. -Body $body
  40. } catch {
  41. $_.Exception | Format-List -Force
  42. }
  43. }
  44.  
  45. $jwt = getAuthenticationToken $AppName $ResourceGroup
  46. setFunctionKey $AppName $FunctionName $KeyName $KeyValue $jwt
  47. Write-Host "Specified key '$KeyName' has been added to $FunctionName"
  48.  
  49. $user = az webapp deployment list-publishing-profiles -n $appName -g $resourceGroup `
  50. --query "[?publishMethod=='MSDeploy'].userName" -o tsv
Add Comment
Please, Sign In to add comment