function - 从 powershell 为 azure 函数创建函数键

标签 function powershell azure

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

最佳答案

目前,没有这样的 Power Shell cmdlet,但您可以使用 Function Api .

使用自动生成的 key 在指定资源上创建或更新 key :

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 .

关于function - 从 powershell 为 azure 函数创建函数键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46521356/

相关文章:

c - 将数组传递给c中的函数

C 递归函数

c++ - 由于引用,重载调用不明确

powershell - 按名称将管道输入发送到不匹配的属性名称

Azure 流量管理器通过不同数据中心的嵌套配置文件实现性能平衡

azure - 通过 LOCATION 语句从 EXTERNAL 中选择

oracle - Oracle函数无法编译

powershell - 跨多个服务器的PowerShell文件比较

Powershell-导出CSV和追加

r - 为什么 DBI ODBC 连接告诉我 CoInitialize 尚未被调用?