2017-10-09 101 views
0

我有下面的代碼在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') 

回答

1

默認的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」} –