2017-10-13 37 views
0

我正在尝试为Azure功能应用设置集成测试。部署进展良好,但我需要一种编程方式来获取默认密钥来运行我的集成测试。通过ARM输出或PowerShell获取Azure功能默认密钥的方法

我试过这里链接的东西 - Get Function & Host Keys of Azure Function In Powershell - 但无法获得在我的ARM部署模板中工作的listsecrets。 Listsecrets无法识别。

有谁知道如何获得与ARM模板和/或PowerShell的密钥?

+0

所以你从字面上连接的答案 – 4c74356b41

+0

我链接到我尝试过的东西,但无法工作,因为它可能已经过时。 –

回答

0

我最终能够在VSTS任务中运行Azure Powershell脚本并将该变量输出到构建密钥。我附上脚本以便其他人可以使用。

#Requires -Version 3.0 

Param(
    [string] [Parameter(Mandatory=$true)] $ResourceGroup, 
    [string] [Parameter(Mandatory=$true)] $FunctionAppName 
) 

$content = Get-AzureRmWebAppPublishingProfile -ResourceGroupName $ResourceGroup -Name $FunctionAppName -OutputFile creds.xml -Format WebDeploy 
$username = Select-Xml -Content $content -XPath "//publishProfile[@publishMethod='MSDeploy']/@userName" 
$password = Select-Xml -Content $content -XPath "//publishProfile[@publishMethod='MSDeploy']/@userPWD" 
$accessToken = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password))) 

$masterApiUrl = "https://$FunctionAppName.scm.azurewebsites.net/api/functions/admin/masterkey" 
$masterKeyResult = Invoke-RestMethod -Uri $masterApiUrl -Headers @{"Authorization"=$accessToken;"If-Match"="*"} 
$masterKey = $masterKeyResult.Masterkey 

$functionApiUrl = "https://$FunctionAppName.azurewebsites.net/admin/host/keys?code=$masterKey" 
$functionApiResult = Invoke-WebRequest -UseBasicParsing -Uri $functionApiUrl 
$keysCode = $functionApiResult.Content | ConvertFrom-Json 
$functionKey = $keysCode.Keys[0].Value 

$saveString = "##vso[task.setvariable variable=FunctionAppKey;]{0}" -f $functionKey 

Write-Host ("Writing: {0}" -f $saveString) 
Write-Output ("{0}" -f $saveString)