Need a good example of getting powershell arguments



  • I'll hit the google later, because I am on other things, but I found that something I touched today could very easily be improved if I can add parameter handling to the powershell script.

    Now, the basics are easy as it is all in the $ARGS variable/object.

    But I want to have some safety checking. because it is easier to do things right the first time.

    Example: I want a parameter to note if I should make the thing being done the default.

    I can pass a 1 like dothing.ps1 1 and I can simply code something to check $ARG[0] eq "1" but that is not very explanatory to the person using the script.

    This is more explanatory dothing.ps1 -default for a command.

    So has anyone seen a good example of parameter handling that I can put into my dothing.ps1 script?



  • Something like this near the top of your script:

    
    param(
    [Parameter(Mandatory=$false)] [string] $adGroup = "Users",
    [Parameter(Mandatory=$false)] [string] $adServer = "MyDomain.Local",
    [Parameter(Mandatory=$false)] [switch] $noTimeStamp, #False if not provided, true if it is
    [Parameter(Mandatory=$true)] [pscredential] $creds=$null
    )
    
    

    For instance, in the snippet above, $adGroup has a default value of "Users" ... and $adServer is "mydomain.local"

    If you want the parameter to be required, set Mandatory = $true (last item above).

    Is that what you're looking for?

    Example scripts:

    https://gitlab.com/dafyre/powershell-utils/raw/master/ActiveDirectory/export-ADGroup.ps1 (a variation of the above snippet comes from here)

    https://gitlab.com/dafyre/powershell-utils/raw/master/Office365/O365-MailTrafficReports.ps1 -- a more complicated example, but a good one anyhow.



  • @JaredBusch said in Need a good example of getting powershell arguments:

    I'll hit the google later, because I am on other things, but I found that something I touched today could very easily be improved if I can add parameter handling to the powershell script.

    Now, the basics are easy as it is all in the $ARGS variable/object.

    But I want to have some safety checking. because it is easier to do things right the first time.

    Example: I want a parameter to note if I should make the thing being done the default.

    I can pass a 1 like dothing.ps1 1 and I can simply code something to check $ARG[0] eq "1" but that is not very explanatory to the person using the script.

    This is more explanatory dothing.ps1 -default for a command.

    So has anyone seen a good example of parameter handling that I can put into my dothing.ps1 script?

    I'm not sure I understand exactly what you mean.

    Taking a guess here, but how I understand is that you'd want to add this at the top of your script:

    [cmdletbinding()]
    param (
            [Parameter()]
            [Switch]$Default
        )
    
    if ($Default) {
        Write-Host "The -Default parameter was specified."
    } else {
        Write-Host "The -Default parameter was NOT specified."
    }
    

    Doing that will give you the following output:

    PS > .\JBTest.ps1 -Default
    The -Default parameter was specified.
    PS > .\JBTest.ps1
    The -Default parameter was NOT specified.
    

    If you want to accept input from a pipeline to work with, let me know.