I'm hoping that I'm not the only one who feels their butt dragging by mid to late afternoon. Let's say that's because we've been thundering through the day and by 3:00 we're a bit out of gas. Yeah, I'll go with that.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
I find myself wanting to close my eyes for only a few minutes to recharge and or at least take the tired edge off. In other words, I just want a quick nap like we had in kindergarten. But I need to make sure I wake up! Since I always have a PowerShell console, I can use it as a quick and dirty alarm clock.
For starters I can use the Start-Sleep cmdlet to wait for X number of seconds. So if I want say a 10 minute timer I can run:
start-sleep –seconds 600
But if my eyes are closed how will know when time is up? A quick solution is to make my computer beep using the [console] .NET class.
[console]::Beep()
So to run this all at once I can enter a command like this:
start-sleep –seconds 600 ; [console]::Beep()
The semi-colon is the end of command marker so what I'm really doing here is executing 2 commands. PowerShell will sleep for 600 seconds and when that command completes, then the Beep() method will run.
But of course it is Friday so let's have a bit more fun with this.
The Beep() method can also take 2 parameters. The first is a tone frequency, and the second is a duration in milliseconds.
[console]::Beep(440,1000)
With this in mind I put together a PowerShell napping script.
#PowerShellNap.ps1 #nap time in minutes Param([int]$Naptime = 1) #define the wakeup time $wake = (Get-Date).AddMinutes($Naptime) #loop until the time is >= the wake up time do { cls Write-host "Ssshhhh...." #trim off the milliseconds write-host ($wake - (Get-Date)).ToString().Substring(0,8) -NoNewline Start-Sleep -Seconds 1 } Until ( (Get-Date) -ge $wake) #Play wake up music [console]::Beep(392,1000) [console]::Beep((329.6*2),1000) [console]::Beep(523.2,1000) Write-Host "`nBack to work sleepy head!" -ForegroundColor Yellow
The script takes a parameter for the number of minutes you need to nap. It then writes a message to anyone who might be walking by your desk to keep quiet. The script then does a countdown of sorts by calculating a timespan between the target end time and the current time. I'm writing the value as a string so that I can strip off the milliseconds.
At the end of the countdown I've recreated a well known chime, at least for those of you in the United States and of a certain age. I'll let you try it out for yourself.
So get a bit more work done today, and when you're ready, take a quick PowerShell Power nap.
C:\scripts\PowerShellNap.ps1 –naptime 10
Enjoy and sweet dreams.
It’s a nice project, but why use the pc speaker?
I would use .net text to speech so I can just put on my headphones and wake up to a voice, instead of the entire office listening to the pc speaker 😉
Something like this:
Add-Type -AssemblyName System.speech
$Speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
$InstalledVoices = $Speak.GetInstalledVoices() | ForEach-Object { $_.VoiceInfo }
$InstalledVoices | ForEach-Object { If($_.Name -eq “Microsoft Hazel Desktop”) { $Speak.SelectVoice(“Microsoft Hazel Desktop”) } }
$Speak.Speak(‘Wake up and assimilate the day. We are the borg. Resistance is futile!’)
$Speak.Speak(‘Wake up and assimilate the day. We are the borg. Resistance is futile!’)
Start-Sleep -Seconds 1
$InstalledVoices | ForEach-Object { If($_.Name -eq “Microsoft David Desktop”) { $Speak.SelectVoice(“Microsoft David Desktop”) } }
$Speak.Speak(“Okay, but how do I turn you off?”)
Start-Sleep -Seconds 1
$InstalledVoices | ForEach-Object { If($_.Name -eq “Microsoft Hazel Desktop”) { $Speak.SelectVoice(“Microsoft Hazel Desktop”) } }
$Speak.Speak(“Believe me, I’m pretty much turned off right now.”)
Start-Sleep -Seconds 2
$InstalledVoices | ForEach-Object { If($_.Name -eq “Microsoft David Desktop”) { $Speak.SelectVoice(“Microsoft David Desktop”) } }
$Speak.Speak(“Time to get back to work and replace someone with a very small shell script!”)
That would be a terrific enhancement! I work at home alone so I don’t worry too much about bothering anyone.