Wednesday 24 December 2008

Get-EvenNumber.ps1

  1. # Requires –Version:2.0 
  2. # Get-EvenNumber.ps1 
  3. # Gets an even number 
  4. # Demonstrates Version 2 functionality 
  5. # Updates Andget-=y Schneider's example at 
  6. # http://get-powershell.com/2008/12/23/industrial-strength-functions-in-powershell-v2-ctp-3/ 
  7.  
  8. function Get-EvenNumber { 
  9. #.Synopsis 
  10. #    Detects and reports on even numbers using the Modulo operator (%) 
  11. #.Description 
  12. #    Takes a pipeline of values and tells you the numbers that are are even  
  13. #    integers. PowerShell detects non-integers.  At the end, this function 
  14. #    tells you how many numbers were processed and how many were even. Non-Inteers 
  15. #    are not reported. 
  16. #.Parameter number 
  17. #    One or more integers - values can come from the pipeline as well    
  18. #.Example 
  19. #    1..10 | Get-EvenNumber 
  20. #.Example 
  21. #    1,2,3,4,"foo" | get-EvenNumber
  22. #    (Generates an error) 
  23.  
  24. param
  25. [Parameter(Position=0,  
  26. Mandatory=$true,  
  27. ValueFromPipeline=$true,  
  28. HelpMessage="Please type a integer")] 
  29. [Alias("integer")] [int32] 
  30.  $number 
  31. # Begin block - just type out a greeting and initialise 
  32. begin {"Beginning of Pipeline";  
  33.        $i=$j=0 
  34.  
  35. process { 
  36.  $i++  
  37.  if (($number % 2) -eq 0) {"$number is even"; $j++} 
  38.  
  39. end { 
  40. "End of Pipeline";  
  41. "{0} integers processed" -f $i;  
  42. "{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 even

PS 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-EvenNumber

End of Pipeline
0 integers processed
0 were even

PS 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-EvenNumber

End of Pipeline
0 integers processed
0 were even

PS C:\foo> Get-Help Get-EvenNumber -Full

NAME
    Get-EvenNumber

SYNOPSIS
    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 well

        Required?                    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: