Create function key for azure function from powershell

后端 未结 1 1862
情话喂你
情话喂你 2021-01-24 21:36

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

相关标签:
1条回答
  • 2021-01-24 21:53

    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.

    0 讨论(0)
提交回复
热议问题