Forgive my ignorance - I am trying to write a powershell cmdlet which takes user input and builds a query uri to an API (one mandatory, 3 opts) - I have kind of got the general idea that I need to use hash tables for dictionary of query strings and parameters.
I'm trying to build $baseurl + $querystring + '=' + $parameter + '&' + $querystring + '=' $value (if not null)
e.g. https://example.com/api?param1=value¶m2=value
so far - and this is very rough, and completely not working:
Function Get-commonURI{ #takes 4 params from user
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String[]]$value1
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String[]]$value2,
[String[]]$value3,
[String[]]$value4
) #end param
}
#put the input into a paramter hash table with the query strings
$Parameters = @{
query = 'querysting1', 'querystring2', 'querystring3', 'querystring4'
values = $value1,$value2.$value2, $value4
}
uri = https://example.com/api?
$HttpValueCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
foreach ($Item in $Parameters.GetEnumerator()) {
#I want to append each query passed in on the cli
foreach ($Value in $Item.Value) {
$ParameterName = $Item.value
$HttpValueCollection.Add($ParameterName, $Value)}
$Request = [System.UriBuilder]($Uri)
$Request.Query = $HttpValueCollection.ToString()
invoke-webrequest $Request.Uri
}
I have something like that written but it's not working - am I even on the right track here? - I'm sure this has been done a million times but don't even know what to google - something tells me I shouldn't set up the hash table with variables. thanks for looking.
$userValue1 = Read-Host "enter first value"
for instance.get-commonURI -param1 value
andget-commonURI -param1 value param2 value
the cmdlet should build the correct uri