Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active October 27, 2020 12:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdhitsolutions/d175558be5486af4cf69224e2184e50b to your computer and use it in GitHub Desktop.
Save jdhitsolutions/d175558be5486af4cf69224e2184e50b to your computer and use it in GitHub Desktop.
A PowerShell WPF script to generate a magic oracle. All in good fun.
#requires -version 5.1
# Usage: .\MagicOracle.ps1
Try {
#try to load the WPF assemblies
Add-Type -AssemblyName PresentationFramework -ErrorAction Stop
Add-Type -AssemblyName PresentationCore -ErrorAction Stop
Add-Type -AssemblyName WindowsBase -ErrorAction Stop
}
Catch {
Write-Warning "Failed to load required WPF assemblies. This script requires Windows PowerShell, or PowerShell 7.x on a Windows platform."
#bail out
return
}
<#
Define an array of answers. One will be randomly selected.
Keep your answers short.
As an alternative, you could store the answers in a json or text file
and import them into this script.
#>
$answers = @(
"It is hard to say"
"No"
"Maybe"
"Yes"
"Absolutely"
"Try again"
"Rejoice"
"Be very afraid"
"Don't give up"
"Only you can know your soul"
"What have you got to lose?"
"Unknown"
"The answer is murky"
"Give up"
"Beware"
"Winter is coming"
"Only you can answer that"
"Sleep on it"
"When you are ready"
"An upgrade is required"
"Please reboot"
"Why not?"
"Roll the dice"
"Be the Master"
"No one said life is fair"
"Prepare"
"A shadow hangs over you"
"Only Jeffrey Snover knows"
"It is so"
"You must be kidding"
"There is no good answer"
"There is no wrong answer"
"Seek help from others"
"This is nonsense"
"Phone home"
"Danger, Will Robinson!"
"Perhaps"
"In your dreams"
"Focus"
"Full speed ahead"
"Who are you kidding?"
"Dream On"
"YOLO"
"You can only try"
"Failure is always an option"
"One can hope"
"Try another day"
"It is written"
"You fool"
"Truly"
"Danger ahead"
"Kismet"
"Be wise"
"Be bold"
"You make your own luck"
"Do you feel lucky?"
"Look for the answer within"
"Fortune favors the brave"
"Karma is kind to you"
"There is no answer"
"There is no spoon"
"I've got a bad feeling about this"
"Highly illogical"
"I'm a magic 8 ball, not a miracle worker"
"More information is required"
"Figure it out for yourself"
"If you work hard"
"In your dreams"
"This is the way"
"It is possible"
"That's just crazy talk"
"May the Force be with you"
"Not the way you imagine"
)
Try {
#create the container form
$form = New-Object System.Windows.Window -ErrorAction stop
}
Catch {
#it is possible to load the WPF assemblies but still not be able to create a form
#this should probably never happen - but just in case.
Write-Warning "Failed to create the form. $($_.Exception.message)"
return
}
#define what it looks like
$form.Title = "Ask the Magic 8 Oracle"
$form.Height = 175
$form.Width = 400
$form.FontFamily = "Chiller"
$form.FontSize = 24
$form.Background = "orange"
$form.add_Loaded({ $textbox.focus() })
#create the stackpanel
$stack = New-Object System.Windows.Controls.StackPanel
#create a label
$label = New-Object System.Windows.Controls.Label
$label.HorizontalAlignment = "Left"
$label.Content = "Ask a simple Yes/No or True/False question"
#add to the stack
$stack.AddChild($label)
$TextBox = New-Object System.Windows.Controls.TextBox
$TextBox.Width = $form.Width - 50
$TextBox.HorizontalAlignment = "Center"
$TextBox.Add_TextChanged( { $btn.IsEnabled = $True })
$stack.AddChild($textbox)
$btn = New-Object System.Windows.Controls.Button
$btn.Content = "_Ask for guidance"
$btn.Width = 150
$btn.HorizontalAlignment = "Center"
$btn.IsEnabled = $False
#define an action scriptblock to run when OK is clicked
$OK = {
if ($btn.Content -match "guidance") {
$answer.content = $answers | Get-Random -Count 1
$btn.content = "_Ask another question"
$textbox.IsEnabled = $False
}
else {
$answer.Content = ""
$TextBox.Text = ""
$textbox.focus()
$textbox.IsEnabled = $True
$btn.content = "_Ask for guidance"
}
}
#add the event handler
$btn.Add_click($OK)
#add the button to the stack
$stack.AddChild($btn)
#create the answer elements
$answer = New-Object System.Windows.Controls.Label
$answer.HorizontalAlignment = "center"
$answer.Width = $form.width - 50
$answer.Height = 100
#add the answer to the stack
$stack.AddChild($answer)
#add the stack to the form
$form.AddChild($stack)
#display the form
[void]$form.showdialog()
@jdhitsolutions
Copy link
Author

image

The button changes text and action based on whether you are asking a question or if one has been answered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment