Wednesday, March 21, 2012

Prize Drawing

We did a simple script with the attendance list piped to a Get-Random to pull winners for the SIG last week.
The problem was for multiple prizes the list didn't get smaller.


So:

001
002
003
004
005
006
007
008
if (-not $list) {
    $list = new-object "System.Collections.Specialized.StringCollection"
    $list.AddRange(("Josh","Will","Bob","John","Jon","Sue"))  
}
$Winner = $list | Get-Random
Write-Host "Please Congratulate: $winner"
$list.Remove($Winner)
$list


The first part will let you set the list once and just F5 for each prize.
If you run out of people -not $list will return true and reset everything.

Or, we can spiff it up:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
if (-not $People) {
    Write-Host "Setting up Values"
    $People = new-object "System.Collections.Specialized.StringCollection"
    $People.AddRange(("Josh","Will","Bob","John","Jon","Sue"))  
    $Prizes = new-object "System.Collections.Specialized.StringCollection"
    $Prizes.AddRange(("T-Shirt","T-Shirt","USB Drive","USB Drive","PowerShell CookBook E-Book","Training CD"))  
}
Function Get-Winner
{
    $Winner = $People | Get-Random
    $Prize = $Prizes | Get-Random
    Write-Host "Please Congratulate $Winner on their new $Prize"
    $People.Remove($Winner)
    $Prizes.Remove($prize)
}

While ($Prizes) { Get-Winner }

# Manual Select and F8 to Reset
#Remove-Variable People

This will just dump a list of winners.  You can remove the While Loop to run single results.  Or even modify to a For-Each and get prizes in order.

No comments:

Post a Comment