将命令参数传递给Azure函数

将命令参数传递给Azure函数

问题描述:

我有下面的代码在Azure函数上运行自己并显示输出。然而,代码是静态的,我需要URL(顶线)作为带有HTTP按需触发器的参数传递。将命令参数传递给Azure函数

大约在经由势在必行绑定的运行时绑定的article here会谈,但它不是100%清楚如何例如通过基于HTTP参数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 

侧面说明,如果你在下面的计算机上的错误... ..

新对象:找不到类型[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') 

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

查询字符串参数将在格式req_query_<parametername>提供给您的脚本变量,所以在你的榜样URL参数上面将使用可访问:$req_query_url

下面的函数是只返回一个简单的例子参数

Out-File -Encoding Ascii -FilePath $res -inputObject "URL parameter $req_query_url" 
+0

是的感谢,我创建一个新的功能时,那种工作了以后,无论如何,默认模板本身所具有的信息{#POST方法:$ REQ $ requestBody =获取内容$ REQ - 原始| ConvertFrom JSON的 $名称= $ requestBody.name #GET方法:每个查询字符串参数是其自己的变量 如果($ req_query_name) {$ 名= $ req_query_name } 出文件-Encoding Ascii码-FilePath $ res -inputObject“Hello $ name”} –