将命令参数传递给Azure Functions

时间:2017-10-09 00:08:53

标签: azure azure-functions

我将以下代码在Azure Functions中自行运行并显示输出。但是代码是静态的,我需要URL(顶行)作为带有HTTP按需触发器的参数传递。

article here在运行时通过命令式绑定讨论绑定,但是如何传递基于HTTP的参数,例如,它并不是100%明确的。 https://myfunction.azurewebsites.net/api/AustereoJustPlaying?url=legacy.scahw.com.au/2classicrock_128.xspf然后使用PowerShell代码中的参数。

# Get the initial metadata for the stream
$url = 'http://legacy.scahw.com.au/2classicrock_128.xspf'
$iwr = Invoke-RestMethod -Uri $url

# Build up the .Net web client
$HttpCompletionOption = 'ResponseContentRead'
$webClient = New-Object System.Net.Http.HttpClient
$webclient.DefaultRequestHeaders.Add('Icy-MetaData', '1')

# Get the Stream URL
$null = $iwr.InnerXml -match '<location>(?<location>.*)<\/location>'
$location = $matches.location
# Fire up the stream
$response = $webClient.GetAsync($location,$HttpCompletionOption)
$null = $webclient.DefaultRequestHeaders.Remove('Icy-MetaData')

# Pause until the stream title actually fires up
Start-Sleep -Seconds 2

# Grab the song
$iwr = Invoke-RestMethod -Uri $url    
$null = $iwr.InnerXml -match '<title>(?<song>.*)<\/title>'

# Kill the stream
$webclient.Dispose()
# Output the song
$matches.song

旁注,如果您在计算机上收到以下错误......

New-Object:找不到类型[System.Net.Http.HttpClient]:验证是否已加载包含此类型的程序集

运行这段代码,似乎你需要'热身'系统以便找到'类型',运行Find-Type httpClient几次似乎唤醒系统实现是的,它确实有这种类型安装。

function Find-Type ([regex]$pattern)
{
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() | 
Select-Object -ExpandProperty FullName | Select-String $pattern
}

Do {
cls
$TypeSearch = Find-Type httpClient
} until ($TypeSearch -match 'System.Net.Http.HttpClient')

1 个答案:

答案 0 :(得分:1)

默认的PowerShell HTTP触发器模板显示了一个示例。

查询字符串参数将作为req_query_<parametername>格式的变量提供给您的脚本,因此在上面的示例中,可以使用以下方法访问URL参数:$req_query_url

以下函数是仅返回参数

的简单示例
Out-File -Encoding Ascii -FilePath $res -inputObject "URL parameter $req_query_url"