1

I'm trying to build a script that has 3 parameters to get the data from an API.

How can I associate the below url with a matrix of combinations?

Invoke-RestMethod -Uri "https:xxxxxxxx?parameter1=$parameter1&parameter2=$parameter2&parameter3=$parameter3" -UseDefaultCredentials

lee-m
  • 2,269
  • 17
  • 29
P90YA
  • 9
  • 1

3 Answers3

0

If your script accepts a single parameter that is an open-ended dictionary (hashtable) of name-value pairs:

param(
  [System.Collections.IDictionary] $NamesAndValues
)

$uri = 'https:xxxxxxxx'
if ($NamesAndValues.Count) { # Non-empty dictionary was passed 

  # Create the collection of query-string entries.
  $queryStringEntries = [System.Web.HttpUtility]::ParseQueryString('')
  foreach ($param in $NamesAndValues.GetEnumerator()) {
    $queryStringEntries.Add($param.Key, $param.Value)
  }

  # Convert the entries to a query string and append it to the URI.
  $uri += '?' + $queryStringEntries.ToString()

}

Invoke-RestMethod -Uri $uri -UseDefaultCredentials 

Sample invocation, using a hashtable literal:

# Results in the following URI:
#      https:xxxxxxxx?parameter1=foo&parameter2=bar%2Fbaz
# Note: -NamesAndValues is positionally implied as the target parameter.
.\YourScript.ps1 @{ param1='foo'; param2='bar/baz' }

If you script has a fixed set of individual, optional parameters:

  • The automatic $PSBoundParameters variable is a dictionary that contains information about what parameters were bound on invocation with what values.

  • Thus, the same technique for constructing the query string as above can be used, with $PSBoundParameters serving as the input dictionary.

# Declare 3 optional (non-mandatory) parameters,
# -parameter1, -parameter2, -parameter3
param(
  $parameter1,
  $parameter2,
  $parameter3
)

# Construct the URI based on whatever parameters were bound.
$uri = 'https:xxxxxxxx'
if ($PSBoundParameters.Count) { # At least 1 parameter was bound.

  # Create the collection of query-string entries.
  $queryStringEntries = [System.Web.HttpUtility]::ParseQueryString('')
  foreach ($param in $PSBoundParameters.GetEnumerator()) {
    $queryStringEntries.Add($param.Key, $param.Value)
  }

  # Convert the entries to a query string and append it to the URI.
  $uri += '?' + $queryStringEntries.ToString()

}

Invoke-RestMethod -Uri $uri -UseDefaultCredentials 

Sample invocation:

# Results in the following URI:
#      https:xxxxxxxx?parameter1=foo&parameter2=bar%2Fbaz
.\YourScript.ps1 -parameter1 'foo' -parameter2 'bar/baz'
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

("x","y","z"),("p","q","r"),("a","b","c") | ForEach-Object {"https://blah?p1={0}&p2={1}=&p3={2}" -f @($_)}

  • 1
    Please [format your post properly](https://stackoverflow.com/help/formatting). – mklement0 Aug 23 '22 at 21:45
  • @mklement0 Sorry, frequent reader but total novice contributor :p Backticks is all I need here, right? – Cainan Barbie Aug 23 '22 at 21:55
  • 1
    Single backticks are best used for _inline_ code formatting. For stand-alone, especially multi-line snippets you'll want code _blocks_. The previously linked article contains everything you need to know. – mklement0 Aug 23 '22 at 22:06
  • As for your solution: while it is elegant, its assumptions are at odds with the OP's request to support a "matrix of combinations", and, as it turns out, not even the parameter _names_ can assumed to be static. Aside from that, note that you cannot blindly embed arbitrary string values in a query string - escaping may be needed. – mklement0 Aug 23 '22 at 22:11
-1

I finally found out the below scheme to create the query. It works good.

$data = @()
$data += 'four'
$data = @('Zero','One','Two','Three')
foreach ( $node in $data )
{
    "Item: [$node]"
}
for ( $index = 0; $index -lt $data.count; $index++)
{
    "Item: [{0}]" -f $data[$index]
}
$JoinArray.Add($value)
$JoinArray
P90YA
  • 9
  • 1
  • It is unclear how this solution relates to what you asked for - where is the conversion to a URL query string, and where are the name-value pairs?. Also, `$JoinArray` and `$value` are never initialized. On a meta note: It is usually better if you continue to work with those that have already provided answers to get to the desired solution. – mklement0 Aug 24 '22 at 17:28
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 29 '22 at 02:34