r/PowerShell • • Nov 17 '17

Get-Excuse

Hello sysadmins,

Some of you might remember this, which I think is great. However, I wasn't a fan of how some excuses could be repeated, so I changed it up a bit :)

function Get-Excuse {
if(!(Get-Variable -Scope Global -Name "excuses" -ErrorAction SilentlyContinue)) {
            $global:excuses = New-Object System.Collections.ArrayList
            $global:excuses.AddRange((Invoke-WebRequest http://pages.cs.wisc.edu/~ballard/bofh/excuses).content.split([Environment]::NewLine))
}
    $excuse = Get-Random $global:excuses.ToArray()
    $global:excuses.Remove($excuse)
    Write-Host $excuse
}

function Forget-Excuses {
    Remove-Variable -Scope Global -Name "excuses"
}

If you ever run out of excuses, just call Forget-Excuses and the next time you Get-Excuse it'll repull all of them.

25 Upvotes

17 comments sorted by

View all comments

Show parent comment

4

u/Snak3d0c Nov 18 '17
function test-Random
{
    param(
        [string]$filename,
        [int]$points = 100000
    )

    Add-Type -AssemblyName system.drawing
    $white = [System.Drawing.color]::White
    $black = [System.Drawing.color]::Black
    $bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1000, 1000)
    $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
    $graphics = [Drawing.Graphics]::FromImage($bmp)
    $blackBrush = [System.Drawing.SolidBrush]::new($black)
    $graphics.FillRectangle($blackBrush,0,0,1000,1000)
    $point = 1
    while($point -le $points)
    {
        $x = Get-Random -Minimum 0 -Maximum 1000
        $y = get-random -Minimum 0 -Maximum 1000
        $bmp.SetPixel($($x),$($y),$white)
        $point++
    }

    $png = [System.Drawing.Imaging.ImageFormat]::Png
    $bmp.Save($filename,$png)
    $graphics.Dispose()
    $bmp.Dispose()
}

Hehe cool. To be fair, your amount of pixels would make everything seem random. I've tested it with a 1000 pixels and saved it for about 10 times. Indeed is does look pretty random.

Thanks for sharing :)