PoshCode Archive  Artifact [b731f7f376]

Artifact b731f7f376b00c8992ecc99f35462407978b183db58fb2038a47c622a4ef6ea7:

  • File Get-Arguments.ps1 — part of check-in [cdb91a83c5] at 2018-06-10 13:05:53 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1625)

# encoding: ascii
# api: powershell
# title: Get-Arguments.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 4.5
# type: function
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2148
# x-archived: 2016-05-17T13:49:02
# x-published: 2011-09-09T21:40:00
#
#
##############################################################################
##
## Get-Arguments
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Uses command-line arguments

#>

param(
    ## The first named argument
    $FirstNamedArgument,

    ## The second named argument
    [int] $SecondNamedArgument = 0
)

Set-StrictMode -Version Latest

## Display the arguments by name
"First named argument is: $firstNamedArgument"
"Second named argument is: $secondNamedArgument"

function GetArgumentsFunction
{
    ## We could use a param statement here, as well
    ## param($firstNamedArgument, [int] $secondNamedArgument = 0)

    ## Display the arguments by position
    "First positional function argument is: " + $args[0]
    "Second positional function argument is: " + $args[1]
}

GetArgumentsFunction One Two

$scriptBlock =
{
    param($firstNamedArgument, [int] $secondNamedArgument = 0)

    ## We could use $args here, as well
    "First named scriptblock argument is: $firstNamedArgument"
    "Second named scriptblock argument is: $secondNamedArgument"
}

& $scriptBlock -First One -Second 4.5