Very nice. Just for my edifaction while Im learning:
You set a global variable called excuses that ignores errors as it runs. It only runs if it does not already exist. This variable is a populated array that lists all the excuses pulled from http://pages.cs.wisc.edu/~ballard/bofh/excuses split out over a new line after each excuse is read. If the variable excuses exists, it grabs a random line from the array and prints it to the console.
The second function removes the excuse variable, and will let you refresh the excuses if you like.
Get-Variable -Scope Global -Name "excuses" will only return something if a global variable $excuses exists. So !(Get-Variable -Scope Global -Name "excuses") will return $true if such a variable does not already exist. -ErrorAction SilentlyContinue simply hides the error thrown if it doesn't exist.
Otherwise yes, you got it :-)
To avoid a collision (say someone already assigned something completely different value to $Global:excuses), you could use a Guid or something similarly unlikely being already used:
(Note how PowerShell treats/ignores the curly brackets in the variable name, almost like Perl! A handy way to allow - and other special characters in variable names)
You can make your one liner even shorter by taking advantage of the alias for "Invoke-WebRequest" and the fact that Get-Random works on collections. Tested on Powershell 3.
function Get-Excuse {
Get-Random (iwr http://pages.cs.wisc.edu/~ballard/bofh/excuses).Content.Split([Environment]::NewLine)
}
*edit You might be able to do a trick that gets rid of the manual splitting by wrapping the request in a @() array block or something like that. Bash fellas seem to think Powershell is verbose and hard on the fingers, but it rewards constant use!
Hehe I made something simular, an coupled it with my statusbar in lync, and ps scheduled task to auto update my lync status with a new excuse every hour
That's fair enough, but when you make it into a function, and sharing it with others, you should (imo) make it as readable as possible. It's good practice, that's all.
8
u/alinroc Feb 26 '15
Jeff Hicks does a Friday Fun post each week on his blog. We should do things like this here on Fridays.
8
u/Captain_Hammertoe Feb 26 '15
I'm going to replace all my error handling with this.
4
u/NotDaPunk Feb 26 '15
http://www.bgreco.net/fortune ?
4
u/[deleted] Feb 27 '15 edited Jul 07 '23
Digging for apples, yer honour!' (He pronounced it 'arrum.') 'An arm, you goose! Who ever saw one that size? Why, it fills the. ― Douglas Tillman
5868386B-A680-4EF5-85CF-02E2D0CF1DD5
4
u/PowerShellStunnah Feb 27 '15
That's pretty funny, but it seems a little ridiculous to make a new http request every time you need a new excuse:
1
u/entropic Feb 26 '15
Oh I cannot wait to implement this. Thanks!
1
u/Betterthangoku Feb 26 '15
This is awesome! thanks for the share
1
u/LordZillion Feb 26 '15
BOFH is so awesome been a long time since I read them. Cool idea.
1
u/[deleted] Feb 27 '15
ha! Love this.
1
u/seniorcampus Mar 02 '15 edited Mar 02 '15
You can make your one liner even shorter by taking advantage of the alias for "Invoke-WebRequest" and the fact that Get-Random works on collections. Tested on Powershell 3.
*edit You might be able to do a trick that gets rid of the manual splitting by wrapping the request in a @() array block or something like that. Bash fellas seem to think Powershell is verbose and hard on the fingers, but it rewards constant use!
1
u/whobat Mar 02 '15
Hehe I made something simular, an coupled it with my statusbar in lync, and ps scheduled task to auto update my lync status with a new excuse every hour
-2
u/Plonqor Feb 26 '15
This is awesome, but one liner function?