从powershell创建azure函数的功能键

时间:2017-10-02 07:44:11

标签: function powershell azure

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

1 个答案:

答案 0 :(得分:1)

目前,没有此类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 -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

注意:您需要创建一个新的服务主体并授予contributor角色。请参阅official document