代码之家  ›  专栏  ›  技术社区  ›  S Raghav Diego Torres Milano

使用powershell脚本获取函数键时出错

  •  1
  • S Raghav Diego Torres Milano  · 技术社区  · 8 年前

    我有一个http触发的azure函数,我需要在部署期间填充其函数键。我目前正在尝试使用给定的过程获取功能键 here (我只调用函数键API,而不是调用主机键API /admin/functions/$functionName/keys?code=$masterKey ). 当我使用KUDU zip API将压缩后的函数代码上传到函数应用程序时,我正在调用API。

    我面临的问题是,在尝试设置功能应用程序的应用程序设置时,我遇到了错误

    调用RestMethod:{“id”:“643ea0b3-5ffe-4683-a8d3-62daec8c8db9”,“requestId”:“ec5461d6-57bc-45dd-b3ac-358602cfa94c”,“statusCode”:500,“errorCode”:0,“message”: 值不能为null。\r\n参数名称:源“}

    但当我在当地尝试时,这是可行的。我怀疑在我进行REST调用以获取函数键时,函数是否尚未部署,从而导致错误。错误消息的含义是什么?我如何修复它?。这是暂时的问题吗?

    更新:代码段

    function get_credentials($resourceGroupName, $functionAppName){
    $creds = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName `
    -ResourceType Microsoft.Web/sites/config -ResourceName "$functionAppName/publishingcredentials" `
    -Action list -ApiVersion 2015-08-01 -Force
    
    $username = $creds.Properties.PublishingUserName
    $password = $creds.Properties.PublishingPassword
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
    return $base64AuthInfo
    }
    function get_master_key($kuduAuthToken, $functionAppName ){
        Write-Host "KUDU token is $kuduAuthToken"
        $apiUrl = "https://$functionAppName.scm.azurewebsites.net/api/functions/admin/masterkey"
        $result = Invoke-RestMethod -Uri $apiUrl -Headers @{"Authorization"="Basic $kuduAuthToken";"If-Match"="*"} 
        $masterKey = $result.masterKey
        Write-Host "master key is $masterKey"
        return $masterKey
    }
    
    function get_function_key($functionAppName, $functionName, $masterKey ){
    
        $apiUrl = "https://$functionAppName.azurewebsites.net/admin/functions/$functionName/keys?code=$masterKey"
        Write-Host "Calling $apiUrl"
        $result = Invoke-RestMethod -Uri $apiUrl 
        Write-Host "Result is $result"
        return $result.keys[0].value
        Write-Host "host token is $result"
    }
    $authToken = get_credentials $resourceGroupName $functionAppName
    $masterKey = get_master_key $authToken $functionAppName
    get_function_key $functionAppName $purgeFunctionName $masterKey
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Bruce Chen    8 年前

    根据您的描述,我在我这边测试了您的工作流,发现它只发生过一次。

    enter image description here

    之后,相同的请求可以按预期工作。

    enter image description here

    根据我的理解,正如你所猜测的,这可能是一个暂时的问题。此外,请确保您的功能可以按预期工作。此外,为了为您的函数生成每个函数键,而不是使用默认的主机键,您还可以利用ARM REST API(例如 https://{functionappname}.azurewebsites.net/admin/functions/{functionname}/keys/{keyname} )为现有函数创建新的函数键。

    enter image description here

    关于创建功能键的详细命令,您可以遵循以下类似的步骤 issue .