Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

PowerShell functions argument catching

2011-04-09

PowerShell is not very object oriented. The support for creating classes is implemented through the Add-Type cmdlet that takes C# code. Here I show how to use Add-Type to define and expose classes that use any part of the .NET Framework. These classes can then be instantiated using the New-Object cmdlet.

Functions are defined using the function keyword. This code defines a function that doesn’t require any arguments:

#Define a function.
function DoSomething()
{
   Write-Output "Hello!"
}

#Call a function.
DoSomething

When defining parameters, you can choose what parameters that need to be strongly typed by adding the desired type name in front of the parameter. Here I have 3 calls to the TwoParams function. The first parameter can be anything, but the second must be and int. Both call 1 and call 2 will run, becase $a can accept a string or an int. Call 3 will fail because $b cannot be a string.

function TwoParams($a, [int]$b)
{
	$b = $b + 1
	Write-Output "a=$a"
	Write-Output "b=$b"
}

#Two correct calls.
Write-Output "Call 1"
TwoParams "Hello!" 10

Write-Output "Call 2"
TwoParams 20 20

Write-Output "Call 3 will fail!"
TwoParams 30 "Hello!"

Default values can be provided, so that omitted arguments still will have a value. In this case, it is assumed that the values 1 and 2 is passed to the function. All parameters are passed by value.

function TestingDefaults($a=1, $b=2)
{
	Write-Output "$a $b"
}

TestingDefaults

Now, let’s look at the strange things. All arguments that are sent in, but not declared in the function head, will be captured by the $args array. In this case, $args in SomeFunction will contain 4 elements, because 4 arguments are sent:

function SomeFunction()
{
	$NumberOfArguments=$args.length
	Write-Output "You gave me $NumberOfArguments arguments."
}

SomeFunction "10" "Hej" 5 10

But here, still 4 arguments are sent, but the first argument is declared, so that only 3 arguments are catched by the $args array:

function SomeFunction($x)
{
	$NumberOfArguments=$args.length
	Write-Output "You gave me $NumberOfArguments arguments."
}

SomeFunction "10" "Hej" 5 10

Categories: PowerShell

One response to “PowerShell functions argument catching”

  1. hemp says:

    With the skills trained so far in this course this is not harder than making the one single module itself….Again the main task is to identify input and output of the various modules.

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips