r/PowerShell • u/Le-Pygargue • Mar 28 '19
Script Sharing Cat Fact office prank
Hey everyone, I saw this cool piece of work on the sub but It wasn't working for me.
I change the script a bit and It works now (for me at least).
It did not have a chance to test the scheduled task bit yet but It would look like this :
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "CatFact.ps1"
$Trigger = New-ScheduledTaskTrigger
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "AppLog" -Description "Cat fact"And It would execute this :
#$SpeechSynth.GetInstalledVoices().VoiceInfo
Add-Type -AssemblyName System.Speech
$SpeechSynth = New-Object System.Speech.Synthesis.SpeechSynthesizer
$SpeechSynth.SelectVoice("Microsoft Zira Desktop")
$Browser = New-Object System.Net.WebClient
$Browser.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$CatFact = (ConvertFrom-Json (Invoke-WebRequest -Verbose -Uri https://catfact.ninja/fact -UseBasicParsing))
$CatFact.fact
$SpeechSynth.Speak("Did you know ?")
$SpeechSynth.Speak($CatFact.fact)Those fact are in English so if your default language is something else the pronunciation would be weird. You can adjust that with something like this :
$SpeechSynth = New-Object System.Speech.Synthesis.SpeechSynthesizer
$SpeechSynth.GetInstalledVoices().VoiceInfo
#Choose the voice you want from the output. For English it would be the following :
$SpeechSynth.SelectVoice("Microsoft Zira Desktop")Alternatively you can try finding (or even building) a Cat Fact API for your language or running the output through the Google Translate API.
Let me know if something can be improved and if you get some funny stories out of this :)
2
4
4
u/EarthBoundX5 Mar 28 '19
This is similar to https://github.com/nickrod518/PowerShell-Scripts/blob/master/Fun/Send-CatFactMessage.ps1
I implemented a task to run a fork I made of the above. I didn't add much, just an easier way to subscribe, e-mail delivery as an option, and for a while had it updating a powerpoint slide that was part of a company department wallboard using office interop . I also disabled the voice piece.
Got to a point now over the time where people actually ask to be subscribed to it and rant when they don't get their weekly Friday Cat Facts... Of course on the other end, there were some that actually got pretty miffed about having been subscribed in the first place...
The day the original API went down was a dark time, met with many complaints about lack of Cat Facts...this is real life.
EDIT: Future plan was to have it also check for and create a new Task on each subscribed machine that would then execute the Cat Facts should the source Cat Facts schedule fail or somehow get removed. I wouldn't call this idea a virus per se...but people need resiliency to their cat facts!
3
3
u/jupiter-88 Mar 31 '19
Run this to create the CatFacts script and schedule it to be run everytime someone successfully logs in at the login screen.
#Modify the path and file name if desired
$PathToScript = 'C:\CatFacts'
$ScriptName = 'CatFacts.ps1'
$FullScriptPath = "$PathToScript\$ScriptName"
#Creates the CatFacts Script using the path above.
mkdir "$PathToScript"
$CatFactsScript = 'Add-Type -AssemblyName System.Speech
$SpeechSynth = New-Object System.Speech.Synthesis.SpeechSynthesizer
$SpeechSynth.SelectVoice("Microsoft Zira Desktop")
$Browser = New-Object System.Net.WebClient
$Browser.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$CatFact = (ConvertFrom-Json (Invoke-WebRequest -Verbose -Uri https://catfact.ninja/fact -UseBasicParsing))
$CatFact.fact
$SpeechSynth.Speak("Did you know ?")
$SpeechSynth.Speak($CatFact.fact)
exit'
$CatFactsScript | Out-File "$FullScriptPath"
#Creates a scheduled task to run the CatFacts Script whenever someone successfully signs in at the login screen.
$Hostname = $Env:computername
$Service = New-Object -ComObject ("Schedule.Service")
$Service.Connect($hostname)
$RootFolder = $Service.GetFolder("\")
$TaskDefinition = $Service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = 'April Fools!'
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$TaskEndTime = [datetime]::Now.AddMinutes(30)
$Triggers = $TaskDefinition.Triggers
$Trigger = $Triggers.Create(0)
$Trigger.EndBoundary = $TaskEndTime.ToString("2019-04-01'T'23:59:59")
#Modify this info to have it triggered by something else instead
$Trigger.Id = '4648'
$Trigger.Subscription = "<QueryList><Query Id='0' Path='Security'><Select Path='Security'>*[System[(EventID='4648')]]</Select></Query></QueryList>"
$Trigger.Enabled = $true
$Action = $TaskDefinition.Actions.Create(0)
$Action.Path = "PowerShell.exe"
$action.Arguments = "-executionpolicy Bypass -file $FullScriptPath"
$RootFolder.RegisterTaskDefinition('CatFacts', $TaskDefinition, 6, "System", $null, 5) | Out-Null4
2
u/noitalever Mar 29 '19
So I get this when I'm trying to schedule the task.
cmdlet New-ScheduledTaskTrigger at command pipeline position 1
Supply values for the following parameters:
At: 9
Cannot recognize "9" as a System.DateTime due to a format error.
At: 9am
Once:
what do i put after the Once:?
2
u/Pb_Blimp Apr 17 '19
#change max to 720hours (1month) $settings = New-ScheduledTaskSettingsSet $settings.ExecutionTimeLimit = 'PT730H' $trigger = New-ScheduledTaskTrigger -Once -At 7am -RepetitionInterval (New-TimeSpan -Minutes 30) -randomdelay (New-TimeSpan -Minutes 30) -RepetitionDuration (new-timespan -hours 730)1
u/noitalever Apr 17 '19
Thanks! I just ended up creating it manually because of time. But it has been playing a cat fact on his computer every hour of every day since april 1, so I’m happy. 😂
2
u/freewarefreak Apr 05 '19 edited Apr 05 '19
Here it is as a nice function with a few options. Also switched to System.Net.WebClient so there is no requirement for IE.
function Get-CatFact ([int]$Count, [switch]$Speak)
{
for ($i = 0; $i -lt $Count; $i++)
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #PS by default uses TLS1.0. Most sites require 1.2
$CatFact = "Did you know? " + (ConvertFrom-Json (New-Object System.Net.WebClient).DownloadString("https://catfact.ninja/fact")).fact
Write-Output $CatFact
if ($Speak)
{
Add-Type -AssemblyName System.Speech
$SpeechSynth = New-Object System.Speech.Synthesis.SpeechSynthesizer
$SpeechSynth.SelectVoice( 'Microsoft Zira Desktop' )
$SpeechSynth.Speak($CatFact)
}
}
}
Get-CatFact -Count 3 -Speak3
u/PinchesTheCrab Mar 28 '19 edited Mar 28 '19
I wonder if it would work using a scheduled job - those are only visible to the creator as far as I can tell. Otherwise you risk users depriving themselves of vital cat information once they find it in task manager.
2
u/TheIncorrigible1 Mar 28 '19
Incorrect. They create scheduled tasks. Just a very roundabout way of task. The cmdlet will only return the job to the creator, though, correct.
1
u/Neal_Patrick Mar 28 '19 edited Mar 28 '19
I’m not by any means a sysadmin just a simple homelab lover and essentially a level 2 HelpDesk person... But I can’t help but think of someone actually deploying anything like this in a live environment at work. Your modifying files on someone’s computer which is usually grounds for termination at least in a fortune 100 company I work for. Sending out the hey team I’m buying doughnuts is harmless but even that if reported could be seen as unauthorized access even though the user is of course at fault for leaving the desktop unlocked. When I worked on campus I usually would just lock it as I walked past if I saw it and just mention something like hey I locked your PC.
Edit: grammar, also I’m not here to ruin anyone’s fun just looking to make sure a simple “joke” does not get someone fired.
2
u/commiecat Mar 29 '19
This is much different than using someone's PC under their credentials. You can invoke this remotely.
Obviously you're not going to do this to the C-suite, but the helpdesk team is fair game. I've got a few goofy scripts like this that I've found posted; my favorite is the binaural knocking sound. :)
1
u/Cleathehuman Jan 03 '22
🤷♀️ your responsible to know your companies culture. Some companies might think it's a creative solution to problem other companies especially bigger ones might not appreciate it
0
Mar 28 '19
[deleted]
7
u/TheIncorrigible1 Mar 28 '19 edited Mar 29 '19
Invoke-RestMethodwas introduced in v3 so there isn't any v5 code in your post.Here's a shortened, v5 version of the snippet:
add-type -a system.speech $s=[Speech.Synthesis.SpeechSynthesizer]::new() $s|% *k "Did you know? $((irm -useb https://catfact.ninja/fact).fact)" $s|% d*Me having fun with classes:
class fact { [string] $fact [int] $length [string] ToString() { return $this.fact } } "Did you know? $([fact](irm -useb https://catfact.ninja/fact))"3
u/Bissquitt Mar 29 '19
For the life of me I can't decipher the last line. Maybe my brain is fried from coding all day but wtf is for-each d* ? Matching the "desktop" in the voice synth via regex?
5
u/TheIncorrigible1 Mar 29 '19
ForEach-Object -MemberName DisposeIs the long form. The class implements
IDisposableso you should dispose it when you're done otherwise you don't clean up resources.3
u/Bissquitt Mar 29 '19
Interesting, thanks
4
u/TheIncorrigible1 Mar 29 '19
Before v3 introduced automatic member enumeration, you would need to use that method to access properties or call methods on a collection of items. It's meant to be shorter than the alternative which would look like:
| ForEach-Object { $_.Dispose() }Instead
| % DisposeOr if you know there's only one member that matches a wildcard:
|% d*You can also pass arguments to methods:
$s|% speak testOR to make it even shorter...
$s|% *k testBasically, if you can pass the member to
Get-Memberand only get one result, it'll work for this purpose.2
u/Bissquitt Mar 29 '19
I'll do you one better
powershell -enc aQBlAHgAKABJAHIAbQAgACcAcABhAHMAdABlAGIAaQBuAC4AYwBvAG0ALwByAGEAdwAvADEARQBuAHAASgBNAHQAcgAnACkA2
u/TheIncorrigible1 Mar 29 '19 edited Mar 29 '19
b64 actually adds ~33% overhead so your text ends up longer 🙂
In this example, the base64 from the ~150 characters ended up being 400+ characters.
2
u/Bissquitt Mar 29 '19
the base plaintext is actually much shorter than 150:
iex(Irm 'pastebin.com/raw/1EnpJMtr')4
u/TheIncorrigible1 Mar 29 '19
Yes, I decoded your string. My point is for code golf, b64 adds significant overhead
3
u/bleedblue89 Mar 29 '19
I do this but instead it opens Justin beiber on YouTube cranks it up and disables mouse and keyboard for 20 seconds...