简体   繁体   中英

Create function key for azure function from powershell

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 .

You can create function keys using Az Cli: az functionapp keys :

Create a function key for an Azure Function app.

 az functionapp keys set ` -g MyResourceGroup ` -n MyFunctionAppName ` --key-type functionKeys ` --key-name MyKeyName ` --key-value MyKeyValue

If the --key-value is not specified, it will be auto-generated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM