Yesterday I was in Montréal, which is about a 2 hours and a half drive from my place. I was down in “La belle province” to give a talk to the Montréal .NET User group (in French) on how PowerShell is an invaluable tool for every developer to have in their back pocket. The talk lasted for over an hour and a half, and there was a lot of very interesting questions and discussions surrounding the topic I was presenting on. In my session I was explaining to the audience how you can build custom reusable modules for PowerShell, leveraging their coding skills. I gave demos of how to create new PowerShell cmdlets using Visual Studio, as well as demos on how to create your own reusable Function that acts just like a normal cmdlet would do.
One of the demo I gave was a custom function I had built and that was simulating a sports event between the Montreal Canadians (hockey team), and another random team in the national hockey league. The function was generating a random score and printing the result back on the screen. Here is an excerpt from my code:
Function Get-SimulatedGameScore
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage=’Name of the home team’)]
[ValidateLength(3,30)]
[string]$homeTeam,
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage=’Name of the visitor team’)]
[Alias(‘Visitor’)]
[ValidateLength(3,30)]
[string]$visitorTeam
)
begin{ Write-Host “Sending off Maggie the Monkey” }
process{
Do
{
$homeScore = Get-Random -min 0 -max 9
$visitorScore = Get-Random -min 0 -max 9
}While($homeScore -eq $visitorScore)
if($homeScore -gt $visitorScore)
{
Write-Host “$homeTeam – $homeScore” -BackgroundColor Green -ForegroundColor Black
Write-Host “$visitorTeam – $visitorScore” -BackgroundColor Red -ForegroundColor Black
}
else
{
Write-Host “$homeTeam – $homeScore” -BackgroundColor Red -ForegroundColor Black
Write-Host “$visitorTeam – $visitorScore” -BackgroundColor Green -ForegroundColor Black
}
}
}
Get-Random “Lightning”, “Red Wings”, “Capitals”, “Sharks”, “Bruins”, “Canucks”, “Flames” | Get-SimulatedGameScore -Visitor “Canadiens”
My Presentation:
http://www.slideshare.net/nikcharlebois/powershell-mtldev-2015