@@ -3,4 +3,144 @@ This is the globl profile file for the Azure Function.
3
3
This file will have been executed first, before any function runs.
4
4
Use this to create a common execution environment,
5
5
but keep in mind that the profile execution time is added to the function startup time for ALL functions.
6
- #>
6
+ #>
7
+
8
+ $global :functionStatusCode = [System.Net.HttpStatusCode ]::OK
9
+ function Convert-AzureFunctionParameter
10
+ {
11
+ <#
12
+ . SYNOPSIS
13
+ Extracts the parameters passed into the rest method.
14
+
15
+ . DESCRIPTION
16
+ Extracts the parameters passed into the rest method of an Azure Function.
17
+ Returns a hashtable, similar to what would be found on a $PSBoundParameters variable.
18
+
19
+ . PARAMETER Request
20
+ The request to process
21
+
22
+ . EXAMPLE
23
+ PS C:\> Convert-AzureFunctionParameter -Request $request
24
+
25
+ Converts the $request object into a regular hashtable.
26
+ #>
27
+ [CmdletBinding ()]
28
+ param (
29
+ $Request
30
+ )
31
+
32
+ $parameters = @ { }
33
+
34
+ foreach ($key in $Request.Query.Keys )
35
+ {
36
+ # Do NOT include the authentication key
37
+ if ($key -eq ' code' ) { continue }
38
+ $parameters [$key ] = $Request.Query .$key
39
+ }
40
+ foreach ($key in $Request.Body.Keys )
41
+ {
42
+ $parameters [$key ] = $Request.Body .$key
43
+ }
44
+
45
+ $parameters
46
+ }
47
+
48
+ function Set-AzureFunctionStatus
49
+ {
50
+ <#
51
+ . SYNOPSIS
52
+ Sets the return status of the function.
53
+
54
+ . DESCRIPTION
55
+ Sets the return status of the function.
56
+ By default, the status is "OK"
57
+
58
+ . PARAMETER Status
59
+ Set the HTTP status for the return from Azure Functions.
60
+ Any status other than OK will cause a terminating error if run outside of Azure Functions.
61
+
62
+ . EXAMPLE
63
+ PS C:\> Set-AzureFunctionStatus -Status BadRequest
64
+
65
+ Updates the status to say "BadRequest"
66
+ #>
67
+ [CmdletBinding ()]
68
+ param (
69
+ [Parameter (Mandatory = $true )]
70
+ [System.Net.HttpStatusCode ]
71
+ $Status
72
+ )
73
+
74
+ $global :functionStatusCode = $Status
75
+ }
76
+
77
+ function Write-AzureFunctionOutput
78
+ {
79
+ <#
80
+ . SYNOPSIS
81
+ Write output equally well from Azure Functions or locally.
82
+
83
+ . DESCRIPTION
84
+ Write output equally well from Azure Functions or locally.
85
+ When calling this command, call return straight after it.
86
+ Use Write-AzureFunctionStatus first if an error should be returned, then specify an error text here.
87
+
88
+ . PARAMETER Value
89
+ The value data to return.
90
+ Either an error message
91
+
92
+ . PARAMETER Serialize
93
+ Return the output object as compressed clixml string.
94
+ You can use ConvertFrom-PSFClixml to restore the object on the recipient-side.
95
+
96
+ . EXAMPLE
97
+ PS C:\> Write-AzureFunctionOutput -Value $result
98
+
99
+ Writes the content of $result as output.
100
+
101
+ . EXAMPLE
102
+ PS C:\> Write-AzureFunctionOutput -Value $result -Serialize
103
+
104
+ Writes the content of $result as output.
105
+ If called from Azure Functions, it will convert the output as compressed clixml string.
106
+
107
+ #>
108
+ [CmdletBinding ()]
109
+ param (
110
+ [Parameter (Mandatory = $true )]
111
+ $Value ,
112
+
113
+ [switch ]
114
+ $Serialize ,
115
+
116
+ [System.Net.HttpStatusCode ]
117
+ $Status
118
+ )
119
+
120
+ if ($PSBoundParameters.ContainsKey (' Status' ))
121
+ {
122
+ Set-AzureFunctionStatus - Status $Status
123
+ }
124
+
125
+ # If not in function, just return value
126
+ if (-not $env: Functions_EXTENSION_VERSION )
127
+ {
128
+ if ($global :functionStatusCode -ne [System.Net.HttpStatusCode ]::OK)
129
+ {
130
+ throw $Value
131
+ }
132
+ return $Value
133
+ }
134
+
135
+ if ($Serialize )
136
+ {
137
+ $Value = $Value | ConvertTo-PSFClixml
138
+ }
139
+
140
+ Push-OutputBinding - Name Response - Value (
141
+ [HttpResponseContext ]@ {
142
+ StatusCode = $global :functionStatusCode
143
+ Body = $Value
144
+ }
145
+ )
146
+ }
0 commit comments