From 6cb3b7582c1602f6e44150061a2432f8cf13c004 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 7 Sep 2018 10:36:01 -0700 Subject: [PATCH 1/3] Fix two bugs --- examples/PSCoreApp/MyHttpTrigger/run.ps1 | 31 ++++++++++-------------- src/PowerShell/PowerShellManager.cs | 10 +++++--- src/Utility/TypeExtensions.cs | 3 ++- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/examples/PSCoreApp/MyHttpTrigger/run.ps1 b/examples/PSCoreApp/MyHttpTrigger/run.ps1 index 93ed1742..7c5706e9 100644 --- a/examples/PSCoreApp/MyHttpTrigger/run.ps1 +++ b/examples/PSCoreApp/MyHttpTrigger/run.ps1 @@ -1,26 +1,21 @@ -# Invoked with Invoke-RestMethod: -# irm http://localhost:7071/api/MyHttpTrigger?Name=Tyler -# Input bindings are added via param block - param($req, $TriggerMetadata) -# If no name was passed by query parameter -$name = 'World' +Write-Verbose "PowerShell HTTP trigger function processed a request." -# You can interact with query parameters, the body of the request, etc. -if($req.Query.Name) { +if($req.Query.Name -or $req.Body.Name) { $name = $req.Query.Name -} - -# you can write to the same streams as you would in a normal PowerShell script -Write-Verbose "Verbose $name" -Verbose -Write-Warning "Warning $name" + if (-not $name) { $name = $req.Body.Name } -# items in the pipeline get logged -$name + $status = 200 + $body = "Hello " + $name +} +else { + $status = 400 + $body = "Please pass a name on the query string or in the request body." +} -# You set the value of your output bindings by assignment `$nameOfOutputBinding = 'foo'` Push-OutputBinding -Name res -Value ([HttpResponseContext]@{ - Body = @{ Hello = $name } - ContentType = 'application/json' + StatusCode = $status + Body = $body }) + diff --git a/src/PowerShell/PowerShellManager.cs b/src/PowerShell/PowerShellManager.cs index a2860839..12947b74 100644 --- a/src/PowerShell/PowerShellManager.cs +++ b/src/PowerShell/PowerShellManager.cs @@ -113,12 +113,14 @@ internal Hashtable InvokeFunction( { // Log everything we received from the pipeline and set the last one to be the ReturnObject Collection pipelineItems = _pwsh.InvokeAndClearCommands(); - foreach (var psobject in pipelineItems) + if (pipelineItems.Count > 0) { - _logger.Log(LogLevel.Information, $"OUTPUT: {psobject.ToString()}"); + foreach (var psobject in pipelineItems) + { + _logger.Log(LogLevel.Information, $"OUTPUT: {psobject.ToString()}"); + } + returnObject = pipelineItems[pipelineItems.Count - 1]; } - - returnObject = pipelineItems[pipelineItems.Count - 1]; } var result = _pwsh.AddCommand("Azure.Functions.PowerShell.Worker.Module\\Get-OutputBinding") diff --git a/src/Utility/TypeExtensions.cs b/src/Utility/TypeExtensions.cs index b469798b..8c3e01a2 100644 --- a/src/Utility/TypeExtensions.cs +++ b/src/Utility/TypeExtensions.cs @@ -70,7 +70,8 @@ public static object ToObject (this TypedData data) switch (data.DataCase) { case TypedData.DataOneofCase.Json: - return JsonConvert.DeserializeObject(data.Json); + var hashtable = JsonConvert.DeserializeObject(data.Json); + return new Hashtable(hashtable, StringComparer.OrdinalIgnoreCase); case TypedData.DataOneofCase.Bytes: return data.Bytes.ToByteArray(); case TypedData.DataOneofCase.Double: From 66b15102dd8f2e41006a838cb2faa0e1171cf87b Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 7 Sep 2018 10:38:17 -0700 Subject: [PATCH 2/3] Add -Verbose to enforce the verbose message --- examples/PSCoreApp/MyHttpTrigger/run.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/PSCoreApp/MyHttpTrigger/run.ps1 b/examples/PSCoreApp/MyHttpTrigger/run.ps1 index 7c5706e9..1589b0cb 100644 --- a/examples/PSCoreApp/MyHttpTrigger/run.ps1 +++ b/examples/PSCoreApp/MyHttpTrigger/run.ps1 @@ -1,6 +1,6 @@ param($req, $TriggerMetadata) -Write-Verbose "PowerShell HTTP trigger function processed a request." +Write-Verbose "PowerShell HTTP trigger function processed a request." -Verbose if($req.Query.Name -or $req.Body.Name) { $name = $req.Query.Name From 14f99099b5bd5b7d822dec0ce972d1d03fb1b7e8 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 7 Sep 2018 11:22:24 -0700 Subject: [PATCH 3/3] Add comments to the example function --- examples/PSCoreApp/MyHttpTrigger/run.ps1 | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/PSCoreApp/MyHttpTrigger/run.ps1 b/examples/PSCoreApp/MyHttpTrigger/run.ps1 index 1589b0cb..e61f034c 100644 --- a/examples/PSCoreApp/MyHttpTrigger/run.ps1 +++ b/examples/PSCoreApp/MyHttpTrigger/run.ps1 @@ -1,11 +1,22 @@ +# Trigger the function by running Invoke-RestMethod: +# (via get method): Invoke-RestMethod -Uri http://localhost:7071/api/MyHttpTrigger?Name=Joe +# (via post method): Invoke-RestMethod ` +# -Uri http://localhost:7071/api/MyHttpTrigger ` +# -Method Post ` +# -Body (ConvertTo-Json @{ Name="Joe" }) ` +# -Headers @{'Content-Type' = 'application/json' }` + +# Input bindings are passed in via param block. param($req, $TriggerMetadata) +# You can write to the Azure Functions log streams as you would in a normal PowerShell script. Write-Verbose "PowerShell HTTP trigger function processed a request." -Verbose -if($req.Query.Name -or $req.Body.Name) { - $name = $req.Query.Name - if (-not $name) { $name = $req.Body.Name } +# You can interact with query parameters, the body of the request, etc. +$name = $req.Query.Name +if (-not $name) { $name = $req.Body.Name } +if($name) { $status = 200 $body = "Hello " + $name } @@ -14,6 +25,7 @@ else { $body = "Please pass a name on the query string or in the request body." } +# You associate values to output bindings by calling 'Push-OutputBinding'. Push-OutputBinding -Name res -Value ([HttpResponseContext]@{ StatusCode = $status Body = $body