从powershell创建天蓝色功能的功能键 [英] Create function key for azure function from powershell

查看:132
本文介绍了从powershell创建天蓝色功能的功能键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能根据powershell脚本为刚刚创建的天青功能创建功能键?
我有一个发布管道为azure函数创建整个环境,并且工作正常,但我缺少的一个部分是该函数的自定义功能键。我不想使用默认键。我可以在门户中创建新的密钥,但我需要通过脚本来完成。

解决方案

目前,没有例如Power Shell cmdlet,但您可以使用 Function Api

使用自动生成的键创建或更新指定资源中的键:

  POST / admin / functions / {functionname} / keys / {keyname} 



<使用以下Power Shell来使用API​​。

  $ tenant =
$ clientId =
$ clientSecret =
$ subscriptionId =

$ body = @ {
grant_type=client_credentials;
client_id= $ clientId;
client_secret= $ clientSecret;
resource=https://management.azure.com/
}
$ resourceGroup =shuiapp
$ name =shuifunction

$ authInfo = Invoke-RestMethod -Urihttps://login.microsoftonline.com/$tenant/oauth2/token-Body $ body -Method Post -Headers @ {Content-Type=application / x -www-form-urlencoded}

$ publishData = Invoke-RestMethod -Urihttps://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Web / sites / $ name / publishxml?api-version = 2016-08-01-Method Post -Headers @ {Authorization=Bearer $($ authInfo.access_token)}

$ userName = $ publishData.publishData.publishProfile [0] .userName
$ password = $ publishData.publishData.publishProfile [0] .userPWD

$ apiBaseUrl =https://$name.scm .azurewebsites.net / api
$ siteBaseUrl =https://$name.azurewebsites.net

#用于验证Kudu
$ base64AuthInfo = [转换]: :ToBase64String([Text.Encoding] :: ASCII.GetBytes(({0}:{1}-f $ usernam e,$ password)))

#调用Kudu / api / functions / admin / token来获得一个可以与函数键API一起使用的JWT
$ jwt = Invoke-RestMethod - Uri$ apiBaseUrl / functions / admin / token-Headers @ {Authorization =(Basic {0}-f $ base64AuthInfo)} -Method GET

#调用函数Key API获取主key
$ x = Invoke-RestMethod -Uri$ siteBaseUrl / admin / host / systemkeys / _master-Headers @ {Authorization =(Bearer {0}-f $ jwt)} -Method GET

$ masterKey = $ x.value

#创建自定义功能键
$ functionname =HttpTriggerPowerShell1
$ v =调用RestMethod -Uri$ siteBaseUrl / admin / functions / $ functionname / keys / shui-Headers @ {Authorization =(Bearer {0}-f $ jwt)} -Method POST
$ v.value

#获取函数键值
$ x = Invoke-RestMethod -Uri$ siteBaseUrl / admin / functions / HttpTriggerPowerShell1 / keys-Headers @ {Authorization =(Bearer {0}-f $ jwt)} - 方法GET

注意:您需要创建一个新的服务主体并给予贡献者角色。请参阅官方文件

Is it possible at all to create a function key for a just created azure function from powershell script? I have got a release pipeline to create the whole environment for azure function and it is working fine but one part I am missing is a custom function key for the function. I don't want to use the default key. I could create the new key in the portal but I need it to be done from the script.

解决方案

Currently, there is no such Power Shell cmdlet, but you could use Function Api.

Creates or updates the key at the specified resource with an auto generated key:

POST /admin/functions/{functionname}/keys/{keyname}

Use the following Power Shell to use API.

$tenant = ""
$clientId = ""
$clientSecret = ""
$subscriptionId = ""

$body = @{
    "grant_type"="client_credentials";
    "client_id"=$clientId;
    "client_secret"=$clientSecret;
    "resource"="https://management.azure.com/"
}
$resourceGroup="shuiapp"
$name="shuifunction"

$authInfo = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenant/oauth2/token" -Body $body -Method Post -Headers @{"Content-Type"="application/x-www-form-urlencoded"} 

$publishData = Invoke-RestMethod -Uri "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$name/publishxml?api-version=2016-08-01" -Method Post -Headers @{"Authorization"="Bearer $($authInfo.access_token)"}

$userName = $publishData.publishData.publishProfile[0].userName
$password = $publishData.publishData.publishProfile[0].userPWD

$apiBaseUrl = "https://$name.scm.azurewebsites.net/api"
$siteBaseUrl = "https://$name.azurewebsites.net"

# For authenticating to Kudu
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))    

# Call Kudu /api/functions/admin/token to get a JWT that can be used with the Functions Key API 
$jwt = Invoke-RestMethod -Uri "$apiBaseUrl/functions/admin/token" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET

# Call Functions Key API to get the master key 
$x = Invoke-RestMethod -Uri "$siteBaseUrl/admin/host/systemkeys/_master" -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method GET

$masterKey = $x.value

# create a custom function key
$functionname="HttpTriggerPowerShell1"
$v=Invoke-RestMethod -Uri "$siteBaseUrl/admin/functions/$functionname/keys/shui" -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method POST
$v.value 

# get function key value
$x = Invoke-RestMethod -Uri "$siteBaseUrl/admin/functions/HttpTriggerPowerShell1/keys" -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method GET

Note: You need create a new service principal and give contributor role. Please refer to the official document.

这篇关于从powershell创建天蓝色功能的功能键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆