Advanced Functions with Powershell –Having some Fun!

Hi Folks ,

Since Powershell V2 CTP3 is out and an important feature called Advanced Functions is a totally new way to create Functions should make your scripts easier . Easier being the key word so far I’m trying to get used to them. In order to learn this new style I decided to go back to an earlier blog I did called Playoff Sunday with powershell. So I decided to do this script over with the new Advanced Function format . I will explain it after I show the script here :


<#
.Synopsis
Outputs random football Score for each team entered on the pipeline
.Description
This script outputs a random score for each football team entered into the pipeline
.Parameter Team
Team
.Example
PS> "Cowboys","Giants" | .\get-footballscore.ps1 | ft -auto


.ReturnValue
[String]


.Link
about_functions
about_functions_advanced
about_functions_advanced_methods
about_functions_advanced_parameters


.Notes
NAME: Get-FootballScore.ps1
AUTHOR: Chris Federico
LASTEDIT: 12/27/2008


#>


param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[string]$Team
)


Process
{


#reset all values ;
$intMaxNumofScores = 0
$intTypeofScore = 0
$s = 0
$FinalScore = 0


#create random max number of scores from 1-7
$intMaxNumofScores = get-random -min 1 -max 5


for ($s = 1;$s -le $intMaxNumofScores;$s++)
{


$intTypeofScore = get-random -min 1 -max 3




switch ($intTypeofScore)
{
1 {$intTypeofScore = 7} #Touchdown
2 {$intTypeofScore = 3} #FieldGoal
3 {$intTypeofScore = 2} #Safety
}
$FinalScore = $FinalScore + $intTypeofScore


}
$obj = New-Object PsObject
$obj | Add-Member NoteProperty Team([string]$Team)
$obj | Add-Member NoteProperty Score([int]$FinalScore)
Write-Output $obj
}#Process

If you break it up into parts its easier to understand . the First part is what is called the in line help (What is in between the <# and #> tags). Now you can provide your own help for your functions and link them to the common parameters already included in other cmdlets like -erroraction or -verbose …

So by providing the above inline help I can then use this to get help :

get-help .\get-footballScore.ps1 -full

NAME
D:\Development\scripts\powershell\Get-FootballScore.ps1

SYNOPSIS
Outputs random football Score

SYNTAX
D:\Development\scripts\powershell\Get-FootballScore.ps1 [-Team] [] [-Verbose] [-Debug] [-ErrorAction []] [-WarningAction [ ce>]] [-ErrorVariable []] [-WarningVariable []] [-OutVariable []] [-OutBuffer []] []

DETAILED DESCRIPTION
This script outputs a random score for each football team entered into the pipeline

PARAMETERS
-Team

Required? true
Position? 0
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters?
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
[String]

NOTES

NAME: Get-FootballScore.ps1
AUTHOR: Chris Federico
LASTEDIT: 12/27/2008

————————– EXAMPLE 1 ————————–

PS> “Cowboys”,”Giants” | .\get-footballscore.ps1| ft -auto

RELATED LINKS
about_functions
about_functions_advanced
about_functions_advanced_methods
about_functions_advanced_parameters

That is some cool Stuff what you can probably do is create a template with the the different parts so you don’t have to create them .

The next part is to declare your parameters . There are different ways to do this depending on how many parameters your passing into the function [parameter(position=0), what position they are in , what type [string] [int] , If the parameters are being passed in from the pipeline ValueFromPipeline=$true, You can also declare a default parameter I believe this way [cmdletdefaultparameter] (?). This is something I will need to get used too . Some of it was already there but other syntax I need to get used too.

The rest of the script I’m basically doing a bunch of random numbers . Its based on the # of scores randomly between 1 and 7 times . You can play with this a bit . Its not scientific at all but the maint thing was that I wanted to practice passing in parameters from the pipeline using advanced functions because this is the way powershell is going . There are so many more things new in V2 CTP 3 … Remoting ,Eventing … Creating Jobs against a bunch of Machines is very promising . Its like parallel processing almost .

Here is an example of a Cowboys vs Giants Score :
“Gaints”,”Cowboys” |D:\Development\scripts\powershell\Get-FootballScore.ps1 | ft -auto

Team Score
—- —–
Gaints 16
Cowboys 13

Subtle but you get the idea ….

Happy Holidays !
Chris

Advertisements