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