Exclude Common Parameters in Filter String

This commit is contained in:
Wes Carroll
2019-07-15 11:48:47 -04:00
parent e6c5effe7c
commit 19b645bccc
+26 -7
View File
@@ -1,22 +1,41 @@
function Get-ZertoApiFilter { function Get-ZertoApiFilter {
[cmdletbinding()] [cmdletbinding()]
[Outputtype([String])]
param( param(
[Parameter( Mandatory = $true, [Parameter( Mandatory = $true,
HelpMessage = "Hashtable that contains filter keys and values" HelpMessage = "Hashtable that contains filter keys and values"
)] )]
[ValidateNotNullOrEmpty()]
[hashtable]$filterTable [hashtable]$filterTable
) )
# Define the start of the return string # Define the start of the return string
[string]$returnString = "?" [string]$returnString = "?"
$commonParameters = @(
"Debug"
"ErrorAction"
"ErrorVariable"
"InformationAction"
"InformationVariable"
"OutVariable"
"OutBuffer"
"PipelineVariable"
"Verbose"
"WarningAction"
"WarningVariable"
"WhatIf"
"Confirm"
)
#Foreach item in the table, process each item #Foreach item in the table, process each item
foreach ( $key in $filterTable.Keys ) { foreach ( $key in $filterTable.Keys ) {
#If this is not the first item added to the string, add the ampersand and filter # If the key is not a common parameter, process it.
if ($returnString.Length -gt 1) { if ($key -notin $commonParameters) {
$returnString = "{0}&{1}={2}" -f $returnString, $key, $filterTable[$key] #If this is not the first item added to the string, add the ampersand and filter
} else { if ($returnString.Length -gt 1) {
#If it is the first item, just add the first item $returnString = "{0}&{1}={2}" -f $returnString, $key, $filterTable[$key]
$returnString = "{0}{1}={2}" -f $returnString, $key, $filterTable[$key] } else {
#If it is the first item, just add the first item
$returnString = "{0}{1}={2}" -f $returnString, $key, $filterTable[$key]
}
} }
} }
# Return the built query String # Return the built query String