Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ARM输出或powershell获取Azure函数默认密钥的方法_Powershell_Azure_Azure Functions - Fatal编程技术网

使用ARM输出或powershell获取Azure函数默认密钥的方法

使用ARM输出或powershell获取Azure函数默认密钥的方法,powershell,azure,azure-functions,Powershell,Azure,Azure Functions,我正在尝试为Azure功能应用程序设置集成测试。部署进展顺利,但我需要一种方法以编程方式获取默认密钥来运行集成测试 我尝试了此处链接的内容,但无法在我的ARM部署模板中使用listsecrets。Listsecrets不被识别 有人知道如何使用ARM模板和/或powershell获取此密钥吗?我最终能够在VSTS任务中运行Azure powershell脚本,并将变量输出到生成密钥。我附加脚本,以便其他人可以使用 #Requires -Version 3.0 Param( [strin

我正在尝试为Azure功能应用程序设置集成测试。部署进展顺利,但我需要一种方法以编程方式获取默认密钥来运行集成测试

我尝试了此处链接的内容,但无法在我的ARM部署模板中使用listsecrets。Listsecrets不被识别


有人知道如何使用ARM模板和/或powershell获取此密钥吗?

我最终能够在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)

在更新了微软的ARM API之后,现在可以直接从ARM部署输出中检索Azure功能键

例子 输出 Outputs属性将包含一个
Newtonsoft.Json.Linq.JObject
条目,其中包含Azure函数的所有键,即主键、系统键和功能键(包括默认键)。不幸的是,JObject与部署变量类型结合在一起有点曲折,需要注意的是,它是区分大小写的。(如果您在PowerShell中工作,则可以将其转换为
哈希表
,以供使用。请参阅下面的奖励。)

奖金 下面的代码消除了额外的
.Value
调用

function Convert-OutputsToHashtable {
  param (
    [ValidateNotNull()]
    [object]$Outputs
  )

  $Outputs.GetEnumerator() | ForEach-Object { $ht = @{} } {
    if ($_.Value.Value -is [Newtonsoft.Json.Linq.JObject]) {
      $ht[$_.Key] = ConvertFrom-Json $_.Value.Value.ToString() -AsHashtable
    } else {
      $ht[$_.Key] = $_.Value.Value
    }
  } { $ht }

}

我只需要通过六个GitHub问题来找到解决方法。
$results = New-AzResourceGroupDeployment...
$keys = results.Outputs.functionKeys.Value.functionKeys.default.Value
function Convert-OutputsToHashtable {
  param (
    [ValidateNotNull()]
    [object]$Outputs
  )

  $Outputs.GetEnumerator() | ForEach-Object { $ht = @{} } {
    if ($_.Value.Value -is [Newtonsoft.Json.Linq.JObject]) {
      $ht[$_.Key] = ConvertFrom-Json $_.Value.Value.ToString() -AsHashtable
    } else {
      $ht[$_.Key] = $_.Value.Value
    }
  } { $ht }

}