Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell URI builder for a sys admin

Tags:

powershell

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&param2=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.

like image 753
Sum1sAdmin Avatar asked Apr 07 '26 13:04

Sum1sAdmin


1 Answers

I'm always a fan of not re-inventing the wheel:

$ub = new-object System.UriBuilder -argumentlist 'http', 'myhost.com', 80, 'mypath/query.aspx', '?param=value'
$ub.Uri.AbsoluteUri
>>>> http://myhost.com/mypath/query.aspx?param=value

Update:

This is a built-in .NET class which has numerous constructors. The one above accepts a protocol, host, port number, path and query string. It seems to handle an empty or null query string, so no need to handle this yourself. For info, the class' constructor can be seen here. In order to retrieve input from the user, you can use Read-Host, e.g.:

[String] $Local:strServer = '';
[String] $Local:strPath   = '';
[String] $Local:strQuery  = '';
[String] $Local:strUri    = '';

while ( $strServer -eq '' ) {
    $strServer = Read-Host -Prompt 'Please enter a server name';
    } #while
while ( $strPath -eq '' ) {
    $strPath   = Read-Host -Prompt 'Please enter a path';
    } #while

# Get query string and ensure it begins with a "?".
$strQuery  = Read-Host -Prompt 'Please enter a query string';
if ( ($strQuery -ne '') -and (! $strQuery.StartsWith('?')) ) { $strQuery = '?' + $strQuery; } 

try {
    $strUri = [System.UriBuilder]::new( 'http', $strServer, 80, $strPath, $strQuery );
    Write-Host -Object ( 'URI is {0}' -f $strUri );
    } #try
catch [System.ArgumentException] {
    # Something went wrong.
    } #catch
like image 92
Simon Catlin Avatar answered Apr 16 '26 04:04

Simon Catlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!