r/PowerShell • • Sep 12 '19

PowerShell All the Things

Post image
505 Upvotes

26 comments sorted by

View all comments

2

u/MadnessDude Sep 13 '19

We're three guys at work, and sometimes we need a "coint-toss" to select which admin does the task, here is our solution..

Function Allocate-Worker {

$Crap = @()

Foreach ($User in 1..9999){
$Players = "Player1","Player2","Player3"
$Random = $Players | Get-random

$CustomObject = New-Object psobject
Add-Member -InputObject $CustomObject -MemberType NoteProperty -Name "WhoWillItBe" -Value $Random
$Crap += $CustomObject
}

    $Player1 = @($Crap | ? {$_.WhoWillItBe -eq "Player1"}).count
    $Player2 = @($Crap | ? {$_.WhoWillItBe -eq "Player2"}).count
    $Player3 = @($Crap | ? {$_.WhoWillItBe -eq "Player3"}).count

    $Player1Calculated = "{0:p1}" -f ($Player1/9999)
    $Player2Calculated = "{0:p1}" -f ($Player2/9999)
    $Player3Calculated = "{0:p1}" -f ($Player3/9999)

    $PrecentageTable = @{
    "Player1" = $Player1Calculated
    "Player2" = $Player2Calculated
    "Player3" = $Player3Calculated
    }

$Precentages = $PrecentageTable.GetEnumerator() | sort -Property Value -Descending
$Precentages | ft

Write-Host "And the winner of this task is $($Precentages.Name | select -First 1)!" -ForegroundColor Magenta

}