PowerShell Advent calendar

calendar.jpg

Here is a short office Advent calendar drawing script.
It demonstrates some basic concepts in PowerShell scripting:
– Creating a arraylist.
– Subtracting from a arraylist.
– Creating a function.
– Simple If/Else statement.
– Counting number of objects in a variable.
– Get-Random.

Im not going to go into dept about the concepts, but provide it more like a basic script you can play with to learn how the concepts work. If any help/explanation is needed, feel free to contact me on Twitter or in the commments.


#Define a ArrayList
[System.Collections.ArrayList]$NamesArray = @('Alex','Alexa','Johnny','James','Jake','Claire','Bart','Cindy','Barbra','Morgan','Jack','Marge','Pamela')

#Draw function you run for each draw
function Draw  {
     If(!$NamesArray){
     "No more names to draw!"
     }
     Else{
     $TodaysWinner = Get-Random $NamesArray.ToArray()
    "$TodaysWinner won the office Advent calendar draw today!"
     $NamesArray.Remove("$TodaysWinner")
     $NumbersLeft = ($NamesArray).Count
     "There are $NumbersLeft more names to draw."
    }
}
Draw

Leave a comment