question

MartinArsovski-9283 avatar image
0 Votes"
MartinArsovski-9283 asked MartinArsovski-9283 commented

How to pass a switch parameter in a http trigger type of Azure function using Powershell

Hello,

I want to use an Azure function to modify the backend pool of a load balancer that will be triggered from an HTTP api.

My functions needs input parameters so it can do validations and use the Az module to modify the Load balancer backend pool.

What i am struggling is that i do not know how to pass a witch parameter as i want the function to have 3 mini functions of removing/adding and setting the backend pool based on a switch parameter provided.

This is the beginning of the function run.ps1 file:

 param($Request, $TriggerMetadata)
 $ErrorActionPreference = 'Stop'
    
 # Write to the Azure Functions log stream.
 Write-Host "Function processed a request."
    
 [string]$resourceGroupName = $Request.Query.resourceGroupName
 if (-not $resourceGroupName) { [string]$resourceGroupName = $Request.Body.resourceGroupName }
 [string]$vnetName = $Request.Query.vnetName
 if (-not $vnetName) { [string]$vnetName = $Request.Body.vnetName }
 [string]$loadBalancerName = $Request.Query.loadBalancerName
 if (-not $loadBalancerName) { [string]$loadBalancerName = $Request.Body.loadBalancerName }
 [string]$backendPoolName = $Request.Query.backendPoolName
 if (-not $backendPoolName) { [string]$backendPoolName = $Request.Body.backendPoolName }
 [array]$backendIps = $Request.Query.backendIps
 if (-not $backendIps) { [array]$backendIps = $Request.Body.backendIps }
 [switch]$Add = [switch]$Request.Query.Add
 if (-not [switch]$Add) { [switch]$Add = [switch]$Request.Body.Add }
 [switch]$Remove = $Request.Query.Remove
 if (-not $Remove) { [switch]$Remove = $Request.Body.Remove }
 [switch]$Set = $Request.Query.Set
 if (-not $Set) { [switch]$Set = $Request.Body.Set }

These are the contents of function.json:

 {
   "bindings": [
     {
       "authLevel": "function",
       "type": "httpTrigger",
       "direction": "in",
       "name": "Request",
       "methods": [
         "get",
         "post"
       ]
     },
     {
       "type": "http",
       "direction": "out",
       "name": "Response"
     }
   ]
 }

This is the call method:

 $iwr = Invoke-WebRequest -Uri "http://localhost:7071/api/modify-lb-backend-pool?resourceGroupName=$resourceGroupName&vnetName=$vnetName&loadBalancerName=$loadBalancerName&backendIps=$backendIps&backendPoolName=$backendPoolName&Set=$Set" -Method Get

And this the error i get:

 EXCEPTION: Cannot convert the "void Add(string key, string value), void IDictionary[string,string].Add(string key, string value), void ICollection[KeyValuePair[string,string]].Add(System.Collections.Generic.KeyValuePair[string,string] item), void IDictionary.Add(System.Object key, System.Object value)" value of type "System.Management.Automation.PSMethod`1[[System.Management.Automation.MethodGroup`4[[System.Func`3[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Management.Automation.VOID, System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Func`3[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Management.Automation.VOID, System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Func`2[[System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Management.Automation.VOID, System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Func`3[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Management.Automation.VOID, System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]" to type "System.Management.Automation.SwitchParameter".
 [2021-07-21T12:06:48.678Z] 
 [2021-07-21T12:06:48.680Z] Exception             : 
 [2021-07-21T12:06:48.681Z]     Type    : System.Management.Automation.ParentContainsErrorRecordException
 [2021-07-21T12:06:48.683Z]     Message : Cannot convert the "void Add(string key, string value), void IDictionary[string,string].Add(string key, string value), void ICollection[KeyValuePair[string,string]].Add(System.Collections.Generic.KeyValuePair[string,string] item), void IDictionary.Add(System.Object key, System.Object value)" value of type "System.Management.Automation.PSMethod`1[[System.Management.Automation.MethodGroup`4[[System.Func`3[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Management.Automation.VOID, System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Func`3[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Management.Automation.VOID, System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Func`2[[System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Management.Automation.VOID, System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Func`3[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Management.Automation.VOID, System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Management.Automation, Version=7.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]" to type "System.Management.Automation.SwitchParameter".
 [2021-07-21T12:06:48.691Z]     HResult : -2146233087
 [2021-07-21T12:06:48.692Z] CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
 [2021-07-21T12:06:48.715Z] FullyQualifiedErrorId : ConvertToFinalInvalidCastException
 [2021-07-21T12:06:48.718Z] InvocationInfo        : 
 [2021-07-21T12:06:48.720Z]     ScriptLineNumber : 17
 [2021-07-21T12:06:48.722Z]     OffsetInLine     : 1
 [2021-07-21T12:06:48.724Z]     HistoryId        : -1
 [2021-07-21T12:06:48.726Z]     ScriptName       : C:\Code\Azure Functions\backend-pool\modify-lb-backend-pool\run.ps1
 [2021-07-21T12:06:48.728Z]     Line             : [switch]$Add = [switch]$Request.Query.Add
 [2021-07-21T12:06:48.730Z] 
 [2021-07-21T12:06:48.732Z]     PositionMessage  : At C:\Code\Azure Functions\backend-pool\modify-lb-backend-pool\run.ps1:17 char:1
 [2021-07-21T12:06:48.735Z]                        + [switch]$Add = [switch]$Request.Query.Add
 [2021-07-21T12:06:48.742Z]                        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 [2021-07-21T12:06:48.745Z]     PSScriptRoot     : C:\Code\Azure Functions\backend-pool\modify-lb-backend-pool
 [2021-07-21T12:06:48.747Z]     PSCommandPath    : C:\Code\Azure Functions\backend-pool\modify-lb-backend-pool\run.ps1
 [2021-07-21T12:06:48.748Z]     CommandOrigin    : Internal
 [2021-07-21T12:06:48.754Z] ScriptStackTrace      : at <ScriptBlock>, C:\Code\Azure Functions\backend-pool\modify-lb-backend-pool\run.ps1: line 17
azure-ad-app-consent
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PramodValavala-MSFT avatar image
1 Vote"
PramodValavala-MSFT answered MartinArsovski-9283 commented

@MartinArsovski-9283 The error is because Add and Remove are methods of IDictionary (type of $Request.Query). Instead of directly access them using ., use [] instead. Also, looks like you need to first cast to a bool.

So, instead of

[switch]$Add = [switch]$Request.Query.Add

try this

[switch]$Add = [switch][bool]$Request.Query["Add"]

and the same Remove as well.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That worked thanks. Do you know by any chance how to pass a switch parameter from the azure function app, as if i put a value to Set it recognizes it as a string.

0 Votes 0 ·