{"id":2016,"date":"2012-01-20T09:28:42","date_gmt":"2012-01-20T14:28:42","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=2016"},"modified":"2012-01-20T09:28:42","modified_gmt":"2012-01-20T14:28:42","slug":"friday-fun-a-powershell-alarm-clock","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/","title":{"rendered":"Friday Fun: A PowerShell Alarm Clock"},"content":{"rendered":"<p>Today's Friday Fun is a continuation of my exploration of ways to use Start-Job. A few weeks ago I wrote about <a href=\"http:\/\/jdhitsolutions.com\/blog\/2012\/01\/using-start-job-as-a-scheduled-task\/\" target=\"_blank\">using Start-Job to create \"scheduled\" tasks<\/a>. I realized I could take this further and turn this into a sort of alarm clock. The goal is to execute at command at a given time, but I wanted to make it easy to specify the time. What I have so far is a function called New-Alarm. I have some other ideas and hope to expand this into a module, but for now I thought I'd toss this out to you and get some feedback.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\nFunction New-Alarm {<\/p>\n<p>[cmdletbinding(SupportsShouldProcess=$True,DefaultParameterSetName=\"Time\")]<\/p>\n<p>Param (<br \/>\n[Parameter(Position=0,ValueFromPipelineByPropertyName=$True)]<br \/>\n[ValidateNotNullorEmpty()]<br \/>\n[string]$Command=\"Notepad\",<br \/>\n[Parameter(Position=1,ValueFromPipelineByPropertyName=$True,ParameterSetName=\"Time\")]<br \/>\n[ValidateNotNullorEmpty()]<br \/>\n[Alias(\"time\")]<br \/>\n[datetime]$Alarm=(Get-Date).AddMinutes(5),<br \/>\n[Parameter(ValueFromPipelineByPropertyName=$True,ParameterSetName=\"Seconds\")]<br \/>\n[int]$Seconds,<br \/>\n[Parameter(ValueFromPipelineByPropertyName=$True,ParameterSetName=\"Minutes\")]<br \/>\n[int]$Minutes,<br \/>\n[Parameter(ValueFromPipelineByPropertyName=$True,ParameterSetName=\"Hours\")]<br \/>\n[int]$Hours,<br \/>\n[Parameter(ValueFromPipelineByPropertyName=$True)]<br \/>\n[Alias(\"init\",\"is\")]<br \/>\n[string]$InitializationScript<br \/>\n)<\/p>\n<p>Process {<\/p>\n<p>if ($seconds) {$Alarm=(Get-Date).AddSeconds($seconds)}<br \/>\nif ($minutes) {$Alarm=(Get-Date).AddMinutes($minutes)}<br \/>\nif ($Hours) {$Alarm=(Get-Date).AddHours($hours)}<\/p>\n<p>Write-Verbose (\"{0} Creating an alarm for {1} to execute {2}\" -f (Get-Date),$Alarm,$Command)<\/p>\n<p>#define a scriptblock that takes parameters. Parameters are validated in the<br \/>\n#function so we don't need to do it here.<br \/>\n$sbText=@\"<br \/>\n    Param ([string]`$Command,[datetime]`$Alarm,[string]`$Init)<\/p>\n<p>    #define a boolean flag<br \/>\n    `$Done=`$False<\/p>\n<p>    #loop until the time is greater or equal to the alarm time<br \/>\n    #sleeping every 10 seconds<br \/>\n    do  {<br \/>\n        if ((get-date) -ge `$Alarm) {<br \/>\n          #run the command<br \/>\n          `$ActualTime=Get-Date<br \/>\n          Invoke-Expression `$Command<br \/>\n          #set the flag to True<br \/>\n          `$Done=`$True<br \/>\n          }<br \/>\n        else {<br \/>\n         sleep -Seconds 10<br \/>\n    }<br \/>\n    } while (-Not `$Done)<\/p>\n<p>    #write an alarm summary object which can be retrieved with Receive-Job<br \/>\n    New-Object -TypeName PSObject -Property @{<br \/>\n      ScheduledTime=`$Alarm<br \/>\n      ActualTime=`$ActualTime<br \/>\n      Command=`$Command<br \/>\n      Initialization=`$Init<br \/>\n    }<br \/>\n\"@<\/p>\n<p>#append metadata to the scriptblock text so they can be parsed out with Get-Alarm<br \/>\n#to discover information for currently running alarm jobs<\/p>\n<p>$meta=@\"<\/p>\n<p>#Alarm Command::$Command<br \/>\n#Alarm Time::$Alarm<br \/>\n#Alarm Init::$InitializationScript<br \/>\n#Alarm Created::$(Get-Date)<\/p>\n<p>\"@<\/p>\n<p>#add meta data to scriptblock text<br \/>\n$sbText+=$meta<\/p>\n<p>Write-Debug \"Scriptblock text:\"<br \/>\nWrite-Debug $sbText<br \/>\nWrite-Debug \"Creating the scriptblock\"<\/p>\n<p>#create a scriptblock to use with Start-Job<br \/>\n$sb=$ExecutionContext.InvokeCommand.NewScriptBlock($sbText)<\/p>\n<p>Try {<br \/>\n    If ($InitializationScript) {<br \/>\n        #turn $initializationscript into a script block<br \/>\n        $initsb=$ExecutionContext.InvokeCommand.NewScriptBlock($initializationscript)<br \/>\n        Write-Verbose (\"{0} Using an initialization script: {1}\" -f (Get-Date),$InitializationScript)<br \/>\n    }<br \/>\n    else {<br \/>\n        #no initialization command so create an empty scriptblock<br \/>\n        $initsb={}<br \/>\n    }<\/p>\n<p>    #WhatIf<br \/>\n    if ($pscmdlet.ShouldProcess(\"$command at $Alarm\")) {<br \/>\n        #create a background job<br \/>\n        Start-job -ScriptBlock $sb -ArgumentList @($Command,$Alarm,$InitializationScript) -ErrorAction \"Stop\" -InitializationScript $Initsb<br \/>\n        Write-Verbose (\"{0} Alarm Created\" -f (Get-Date))<br \/>\n    }<br \/>\n}<\/p>\n<p>Catch {<br \/>\n    $msg=\"{0} Exception creating the alarm job. {1}\" -f (Get-Date),$_.Exception.Message<br \/>\n    Write-Warning $msg<br \/>\n}<br \/>\n} #Process<\/p>\n<p>} #end function<br \/>\n<\/code><\/p>\n<p>The function includes full help.<\/p>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help-300x197.png\" alt=\"\" title=\"new-alarm-help\" width=\"300\" height=\"197\" class=\"aligncenter size-medium wp-image-2017\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help-300x197.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help-1024x674.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help.png 1157w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>To use the function you specify a command string to execute at a given time. The default's are to run Notepad in 5 minutes. You can either specify an exact time.<\/p>\n<p><code><br \/>\nPS C:\\> new-alarm \"get-process | out-file c:\\work\\noonprocs.txt\" -alarm \"12:00PM\"<br \/>\n<\/code><\/p>\n<p>Or X number of seconds, minutes or hours.<\/p>\n<p><code><br \/>\nPS C:\\> $s='$f=[system.io.path]::GetTempFilename(); \"Hey! Are you paying attention??\" > $f;start-process notepad $f -wait;del $f'<br \/>\nPS C:\\> new-alarm $s -minutes 15 -verbose<br \/>\n<\/code><\/p>\n<p>The first command defines a command string, $s. This creates a temporary file, writes some text to it, displays it with Notepad and then deletes it. The second command creates a new alarm that will invoke the expression in 15 minutes.<\/p>\n<p>For now, the command is passed as text. This is so that I can create an internal scriptblock. I use a Do loop to compare the current time to the alarm time. When the time is right, the command string is executed using Invoke-Expression.<\/p>\n<p><code lang=\"PowerShell\"><br \/>\n$sbText=@\"<br \/>\n    Param ([string]`$Command,[datetime]`$Alarm,[string]`$Init)<\/p>\n<p>    #define a boolean flag<br \/>\n    `$Done=`$False<\/p>\n<p>    #loop until the time is greater or equal to the alarm time<br \/>\n    #sleeping every 10 seconds<br \/>\n    do  {<br \/>\n        if ((get-date) -ge `$Alarm) {<br \/>\n          #run the command<br \/>\n          `$ActualTime=Get-Date<br \/>\n          Invoke-Expression `$Command<br \/>\n          #set the flag to True<br \/>\n          `$Done=`$True<br \/>\n          }<br \/>\n        else {<br \/>\n         sleep -Seconds 10<br \/>\n    }<br \/>\n    } while (-Not `$Done)<\/p>\n<p>    #write an alarm summary object which can be retrieved with Receive-Job<br \/>\n    New-Object -TypeName PSObject -Property @{<br \/>\n      ScheduledTime=`$Alarm<br \/>\n      ActualTime=`$ActualTime<br \/>\n      Command=`$Command<br \/>\n      Initialization=`$Init<br \/>\n    }<br \/>\n\"@<br \/>\n<\/code><\/p>\n<p>I also add some metadata to the script block which gets written as the job's result.<\/p>\n<p><code><br \/>\n#append metadata to the scriptblock text so they can be parsed out with Get-Alarm<br \/>\n#to discover information for currently running alarm jobs<\/p>\n<p>$meta=@\"<\/p>\n<p>#Alarm Command::$Command<br \/>\n#Alarm Time::$Alarm<br \/>\n#Alarm Init::$InitializationScript<br \/>\n#Alarm Created::$(Get-Date)<\/p>\n<p>\"@<\/p>\n<p>#add meta data to scriptblock text<br \/>\n$sbText+=$meta<\/p>\n<p>Write-Debug \"Scriptblock text:\"<br \/>\nWrite-Debug $sbText<br \/>\nWrite-Debug \"Creating the scriptblock\"<\/p>\n<p>#create a scriptblock to use with Start-Job<br \/>\n$sb=$ExecutionContext.InvokeCommand.NewScriptBlock($sbText)<br \/>\n<\/code><\/p>\n<p>Finally, the alarm function allows for an initialization command, like you might use with Start-Job. This permits you to run commands such as importing modules or dot sourcing scripts.  I have a function that displays a VB style message box. Here's how I might use it as an alarm job.<\/p>\n<p><code><br \/>\nPS C:\\> new-alarm \"get-messagebox 'It is time for that thing' -title 'Alert!'\" -init \". c:\\scripts\\get-messagebox.ps1\" -min 5<br \/>\n<\/code><\/p>\n<p>In 5 minutes the alarm will go off and I'll get this.<\/p>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/alert.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/alert.png\" alt=\"\" title=\"alert\" width=\"227\" height=\"161\" class=\"aligncenter size-full wp-image-2018\" \/><\/a><\/p>\n<p>Remember, the function is creating new jobs with the Start-Job cmdlet. Which means I can get job results. <\/p>\n<p><code><br \/>\nPS C:\\> receive-job 7 -keep<\/p>\n<p>Initialization : . c:\\scripts\\get-messagebox.ps1<br \/>\nActualTime     : 1\/20\/2012 8:47:07 AM<br \/>\nScheduledTime  : 1\/20\/2012 8:47:06 AM<br \/>\nCommand        : get-messagebox 'It is time for that thing' -title 'Alert!'<br \/>\nRunspaceId     : d3461b78-11ce-4c84-a8ab-9e3fcd482637<br \/>\n<\/code><\/p>\n<p>What do you think? As I said, I have a few more ideas and there are certainly a few tweaks I can make even to this code. I've added my Get-MessageBox function in case you want to toy with that.  Download <a href='http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/AlarmScripts.zip'>AlarmScripts.zip<\/a> and let me know what you think.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Friday Fun is a continuation of my exploration of ways to use Start-Job. A few weeks ago I wrote about using Start-Job to create &#8220;scheduled&#8221; tasks. I realized I could take this further and turn this into a sort of alarm clock. The goal is to execute at command at a given time, but&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[271,4,75],"tags":[230,202,534,333],"class_list":["post-2016","post","type-post","status-publish","format-standard","hentry","category-friday-fun","category-powershell","category-powershell-v2-0","tag-fridayfun","tag-jobs","tag-powershell","tag-start-job"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Friday Fun: A PowerShell Alarm Clock &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Friday Fun: A PowerShell Alarm Clock &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Today&#039;s Friday Fun is a continuation of my exploration of ways to use Start-Job. A few weeks ago I wrote about using Start-Job to create &quot;scheduled&quot; tasks. I realized I could take this further and turn this into a sort of alarm clock. The goal is to execute at command at a given time, but...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2012-01-20T14:28:42+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help-300x197.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Friday Fun: A PowerShell Alarm Clock\",\"datePublished\":\"2012-01-20T14:28:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/\"},\"wordCount\":388,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/01\\\/new-alarm-help-300x197.png\",\"keywords\":[\"FridayFun\",\"Jobs\",\"PowerShell\",\"Start-Job\"],\"articleSection\":[\"Friday Fun\",\"PowerShell\",\"PowerShell v2.0\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/\",\"name\":\"Friday Fun: A PowerShell Alarm Clock &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/01\\\/new-alarm-help-300x197.png\",\"datePublished\":\"2012-01-20T14:28:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/01\\\/new-alarm-help.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2012\\\/01\\\/new-alarm-help.png\",\"width\":\"1157\",\"height\":\"762\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/2016\\\/friday-fun-a-powershell-alarm-clock\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Friday Fun\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/friday-fun\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Friday Fun: A PowerShell Alarm Clock\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Friday Fun: A PowerShell Alarm Clock &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/","og_locale":"en_US","og_type":"article","og_title":"Friday Fun: A PowerShell Alarm Clock &#8226; The Lonely Administrator","og_description":"Today's Friday Fun is a continuation of my exploration of ways to use Start-Job. A few weeks ago I wrote about using Start-Job to create \"scheduled\" tasks. I realized I could take this further and turn this into a sort of alarm clock. The goal is to execute at command at a given time, but...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/","og_site_name":"The Lonely Administrator","article_published_time":"2012-01-20T14:28:42+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help-300x197.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Friday Fun: A PowerShell Alarm Clock","datePublished":"2012-01-20T14:28:42+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/"},"wordCount":388,"commentCount":0,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help-300x197.png","keywords":["FridayFun","Jobs","PowerShell","Start-Job"],"articleSection":["Friday Fun","PowerShell","PowerShell v2.0"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/","name":"Friday Fun: A PowerShell Alarm Clock &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help-300x197.png","datePublished":"2012-01-20T14:28:42+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/01\/new-alarm-help.png","width":"1157","height":"762"},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2016\/friday-fun-a-powershell-alarm-clock\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Friday Fun","item":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},{"@type":"ListItem","position":2,"name":"Friday Fun: A PowerShell Alarm Clock"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":3990,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3990\/friday-fun-creating-powershell-scripts-with-powershell\/","url_meta":{"origin":2016,"position":0},"title":"Friday Fun: Creating PowerShell Scripts with PowerShell","author":"Jeffery Hicks","date":"September 5, 2014","format":false,"excerpt":"Sometimes PowerShell really does seem like magic. Especially when you can use it to handle complicated or tedious tasks, such as creating a PowerShell script. I know many an IT Pro who want to script but without having to actually write a script. Well, I'm not sure that is realistic,\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"012914_1704_CreatingCIM1.png","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/01\/012914_1704_CreatingCIM1-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":4471,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4471\/friday-fun-tickle-me-powershell\/","url_meta":{"origin":2016,"position":1},"title":"Friday Fun: Tickle Me PowerShell!","author":"Jeffery Hicks","date":"July 24, 2015","format":false,"excerpt":"I work at home for myself which means I have to act as my own assistant, reminding me of important events and tasks. And sometimes I need a little help. So why not use PowerShell? In the past I've used and written about using a background job to wait until\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2503,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2503\/friday-fun-test-powershell-command-name\/","url_meta":{"origin":2016,"position":2},"title":"Friday Fun &#8211; Test PowerShell Command Name","author":"Jeffery Hicks","date":"October 12, 2012","format":false,"excerpt":"Earlier this week I exchanged a few tweets with @jonhtyler about coming up with a proper name for a PowerShell function he was developing. The suggested best practice is to use the Verb-Noun naming convention, using an accepted verb. You can see the verbs with the Get-Verb cmdlet. So I\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3377,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3377\/friday-fun-get-day-of-the-year-with-powershell\/","url_meta":{"origin":2016,"position":3},"title":"Friday Fun: Get Day of the Year with PowerShell","author":"Jeffery Hicks","date":"August 30, 2013","format":false,"excerpt":"Earlier this week I was having some fun with @EnergizedTech on Twitter, playing around with dates in PowerShell. I'm not even sure where we started but the experience got me thinking and it's Friday so let's have some fun. While I can easily find out what the day of the\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"calendar","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/08\/calendar.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":4830,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4830\/friday-fun-a-powershell-nap\/","url_meta":{"origin":2016,"position":4},"title":"Friday Fun: A PowerShell Nap","author":"Jeffery Hicks","date":"January 22, 2016","format":false,"excerpt":"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. I find myself wanting to close my eyes\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"antique-watch-150x225","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2016\/01\/antique-watch-150x225_thumb.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":4048,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4048\/friday-fun-pick-up-milk\/","url_meta":{"origin":2016,"position":5},"title":"Friday Fun: Pick Up Milk","author":"Jeffery Hicks","date":"September 26, 2014","format":false,"excerpt":"Sometimes when working on a PowerShell problem, you might have to come to the conclusion that PowerShell is not the right tool for the job. There are some tasks or applications that simply don't lend themselves to automation or PowerShell. This isn't necessarily a limitation in PowerShell, nor would I\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"sticky-graphic","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/sticky-graphic-150x150.jpg?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2016"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2016\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}