none
Creating Powershell Functions - Best Practice RRS feed

  • Question

  • Hello,

    I intend to make PowerShell cmdlets/functions that would allow me to query a database by a value that I specify, and return a specific number of results. Meanwhile, I don't want to lose the flexibility to manipulate the data down the pipeline. Here is an example:

    function Get-Data { 

    param(

    [string]$data

    $query = Invoke-RestMethod -Method Get -Uri url.com/$data -WebSession $currentsession 

    $query | Select-Object -Property $CustomTable

    }

    The use would be able to enter this into the command line.

    PS > Get-Data 'searchterm' 'number of results' More specifically PS > Get-Data FooBar 10 | Out-GridView

    #Returns a maximum of 10 FooBars in ogv


    I wanted to consider splitting the $data variable into two, but the first value might contain spaces which would cause issues.

    So if I wanted to say

    PS >Get-Data Foo Bar 100

    The second part of the data variable would account for 'Bar' and not 100.

    Thursday, January 5, 2017 4:12 PM

Answers

  • Your question is not clear. If you always want to pass two strings, then create two parameters.

    If you want to accept multiple (1 or more) strings as input, then specify that the parameter accepts an array.

    See the marked answer to this question for how to use an array for a parameter, if that's what you want to do.


    -- Bill Stewart [Bill_Stewart]


    Thursday, January 5, 2017 5:44 PM
    Moderator

All replies

  • If you're going to write a function that accepts a parameter value with a space, or spaces, then the function's users are going to need to be educated enough to know to wrap the value in quotes (single preferred, by default).
    • Proposed as answer by jrv Thursday, January 5, 2017 5:04 PM
    Thursday, January 5, 2017 4:51 PM
  • If you say this:


    Get-Data Foo Bar 100

    You are passing three parameters to your function (two strings and an integer).

    PowerShell is not going to somehow magically understand that two separate strings are a single string unless you quote the string for its parser.


    -- Bill Stewart [Bill_Stewart]


    Thursday, January 5, 2017 5:16 PM
    Moderator
  • Thank you for your response, I understand that PowerShell wouldn't magically understand that 'two separate strings are a single string unless.' I was merely asking if there was a better practice then actually splitting the strings to accomplish this.
    Thursday, January 5, 2017 5:27 PM
  • Your question is not clear. If you always want to pass two strings, then create two parameters.

    If you want to accept multiple (1 or more) strings as input, then specify that the parameter accepts an array.

    See the marked answer to this question for how to use an array for a parameter, if that's what you want to do.


    -- Bill Stewart [Bill_Stewart]


    Thursday, January 5, 2017 5:44 PM
    Moderator
  • This is exactly what I was looking for.
    Thursday, January 5, 2017 5:52 PM