- # Requires –Version:2.0
- # Get-EvenNumber.ps1
- # Gets an even number
- # Demonstrates Version 2 functionality
- # Updates Andget-=y Schneider's example at
- # http://get-powershell.com/2008/12/23/industrial-strength-functions-in-powershell-v2-ctp-3/
- function Get-EvenNumber {
- #.Synopsis
- # Detects and reports on even numbers using the Modulo operator (%)
- #.Description
- # Takes a pipeline of values and tells you the numbers that are are even
- # integers. PowerShell detects non-integers. At the end, this function
- # tells you how many numbers were processed and how many were even. Non-Inteers
- # are not reported.
- #.Parameter number
- # One or more integers - values can come from the pipeline as well
- #.Example
- # 1..10 | Get-EvenNumber
- #.Example
- # 1,2,3,4,"foo" | get-EvenNumber
- # (Generates an error)
- param (
- [Parameter(Position=0,
- Mandatory=$true,
- ValueFromPipeline=$true,
- HelpMessage="Please type a integer")]
- [Alias("integer")] [int32]
- $number
- )
- # Begin block - just type out a greeting and initialise
- begin {"Beginning of Pipeline";
- $i=$j=0
- }
- process {
- $i++
- if (($number % 2) -eq 0) {"$number is even"; $j++}
- }
- end {
- "End of Pipeline";
- "{0} integers processed" -f $i;
- "{0} were even" -f $j
- }
- }
This script produces the following output:
PS C:\foo> 1..10 | Get-EvenNumber
Beginning of Pipeline
2 is even
4 is even
6 is even
8 is even
10 is even
End of Pipeline
10 integers processed
5 were evenPS C:\foo> -22,200000,2 | Get-EvenNumber
Beginning of Pipeline
-22 is even
200000 is even
2 is even
End of Pipeline
3 integers processed
3 were even
PS C:\foo> "foo" | Get-EvenNumber
Begining of Pipeline
Get-EvenNumber : Cannot process argument transformation on parameter 'number'. Cannot convert value "foo" to type "System.Int32". Error:
"Input string was not in a correct format."
At line:1 char:23
+ "foo" | Get-EvenNumber <<<<
+ CategoryInfo : InvalidData: (foo:String) [Get-EvenNumber], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-EvenNumberEnd of Pipeline
0 integers processed
0 were evenPS C:\foo> 123456789012 | Get-EvenNumber
Begining of Pipeline
Get-EvenNumber : Cannot process argument transformation on parameter 'number'. Cannot convert value "123456789012" to type "System.Int32"
. Error: "Value was either too large or too small for an Int32."
At line:1 char:30
+ 123456789012 | Get-EvenNumber <<<<
+ CategoryInfo : InvalidData: (123456789012:Int64) [Get-EvenNumber], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-EvenNumberEnd of Pipeline
0 integers processed
0 were evenPS C:\foo> Get-Help Get-EvenNumber -Full
NAME
Get-EvenNumberSYNOPSIS
Detects and reports on even numbers using the Modulo operator (%)SYNTAX
Get-EvenNumber [-number] [<Int32>] [-Verbose] [-Debug] [-ErrorAction [<ActionPreference>]] [-WarningAction [<ActionPreference>]] [-Er
rorVariable [<String>]] [-WarningVariable [<String>]] [-OutVariable [<String>]] [-OutBuffer [<Int32>]] [<CommonParameters>]DETAILED DESCRIPTION
Takes a pipeline of values and tells you the numbers that are are even
integers. PowerShell detects non-integers. At the end, this function
tells you how many numbers were processed and how many were even. Non-Inteers
are not reported.PARAMETERS
-number
One or more integers - values can come from the pipeline as wellRequired? true
Position? 0
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters?<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug,
-ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer and -OutVariable. For more information, type,
"get-help about_commonparameters".INPUT TYPE
RETURN TYPE
-------------------------- EXAMPLE 1 --------------------------
1..10 | Get-EvenNumber
-------------------------- EXAMPLE 2 --------------------------
1,2,3,4,"foo" | get-EvenNumber
(Generates an error)
No comments:
Post a Comment