Private/Add-UriQueryParam.ps1
|
function Add-UriQueryParam { param( [Parameter(Mandatory, ValueFromPipeline)] [string]$Uri, [Parameter(Mandatory, Position = 0)] [string]$Query, [Parameter()] [switch]$ConvertWildcards ) begin {} process { if ($ConvertWildcards) { $Query = $Query.Replace('*', '%').Replace('?', '_') } $JoinChar = if ($Uri.Contains('?')) { '&' } else { '?' } $Key, $Value = $Query -split '=' if ($Uri -match "[?&]$Key=*") { Write-Debug $('Replacing existing query parameter {0} with {1}.' -f $Key, $Value) $Uri -replace "($Key=)[^&]*", "$Key=$Value" } else { $Uri, $Query -join $JoinChar } } end {} } |