3

I have created myself a PowerShell program to act as my alarm clock in the morning. I have Task Scheduler executing it on a time trigger. The problem I am having is a lack of consistency.

Sometimes it will run properly without any interference, other times it will open PowerShell, error out and close immediately without executing (no error code).

When I execute it myself with a double click, it seems to work just fine.

Execution Policy = All-Signed

Task Scheduler

Trigger Tab:

Trigger: Daily
Details: At 8:00 AM every Day
Status: Enabled

Action Tab:

Action: Start a Program
Program/Script: PowerShell.exe 
Add arguments: -NoExit D:\Programs\AlarmClock\AlarmClockScript.ps1

Script:

#define loop start state

$Snoozevar = 'Yes' 

#Import form module (for menu)

[reflection.assembly]::LoadWithPartialName("System.Windows.forms") | Out-Null

#Menu

    $snoozeTxtBox = New-Object System.Windows.Forms.Button
    $snoozeTxtBox.Text = 'Snooze'
    $snoozeTxtBox.Location = '50,15'
    $snoozeTxtBox.Size = '60,23'
    $snoozeTxtBox.DialogResult = [System.Windows.Forms.DialogResult]::Yes  # 'Snooze' = Yes

    $quitTxtBox = New-Object System.Windows.Forms.Button
    $quitTxtBox.Text = 'I`m Up'
    $quitTxtBox.Location = '125,15'
    $quitTxtBox.Size = '50,23'
    $quitTxtBox.DialogResult = [System.Windows.Forms.DialogResult]::No     # 'I`m Up' = No

    $basicForm = New-Object System.Windows.Forms.Form
    $basicForm.StartPosition = 'CenterScreen'
    $basicForm.Size = '250,100'
    $basicForm.Controls.Add($snoozeTxtBox)
    $basicForm.Controls.Add($quitTxtBox)
    $basicForm.TopMost = $true

while ($Snoozevar -eq 'Yes'){

    Start-Process "D:\Programs\Winamp\winamp.exe" /PLAY                    # Start Winamp /autoplay

    Start-Process D:\Programs\nircmd\nircmd.exe -ArgumentList " setsysvolume 65535" #Max Volume

    $Snoozevar = $basicForm.ShowDialog()                                   # Call Menu, assign output to $Snoozevar

    $pro = Get-Process -Name "winamp"                                      # Kill winamp post menu selection
    Stop-Process -Id $pro.Id
    $pro = ""
    
    if ($Snoozevar -eq 'No'){                                              # Clean up powershell
        $pro = Get-Process -Name powershell
        Stop-Process $pro
    } #end if

    $rngvar = Get-Random -Minimum 540 -Maximum 720                         # Time to Snooze (9-12 minutes)
    Start-Sleep -Seconds $rngvar

} #end while
# SIG # Begin signature block
...
# SIG # End signature block
4
  • Is "Run whether user is logged on or not checked" ? If not, task won't run if you are not logged in. If yes, task won't run interactlively. Is it running on a PC or laptop ? Default condition is to only run if the computer is on AC power. Commented Jul 14, 2020 at 3:17
  • 2
    Try to add Start-Transcript -Path "Some\Path\AlarmLog_$(get-date -f 'yyyyMMdd').txt" / Stop-Transcript at the top and bottom of your script to record what is happening... You might be able to see what's failing or gain more insights from there. Commented Jul 14, 2020 at 3:21
  • In Task Scheduler, what does the Last Run Result column show for that task after a failure? If you right-click the task and select Run does it always succeed? Commented Jul 14, 2020 at 3:37
  • 1
    @SagePourpre Thank you for showing me Start/Stop Transcript. I discovered my winamp is crashing thanks to it failing $pro = Get-Process -Name "winamp". All manual runs have failed tonight, leaving me with Last Run Result: 0xFFFFFFFF. I have modified the task settings to give it the best chance to run and I'm taking a look at winamp now. Commented Jul 14, 2020 at 4:34

1 Answer 1

3

Here's a summary of the things that can be done to diagnose an inconsistend scheduled task execution.

  • Since your task is interactive (have a form), Run whether user is logged on or not should be left unchecked. While you'd normally want it checked most of the time, tasks that interact with the user (popup / forms / etc...) won't work properly if thus option is checked.

  • Add Start-Transcript -Path "Some\Path\AlarmLog_$(get-date -f 'yyyyMMdd').txt at the beginning of your file and Stop-Transcript at the end to gain more insight on when it fail

  • Make sure to check the Conditions tab as there are additional constraint that could affect task execution (eg: By default, task will not execute if on battery power)

  • If the task is running under a different user or in a different context (eg: with Highest priviledges), try to execute your script in that context to see if it fail (for instance, start Vscode / ISE using that context and run the task)

If you have multiple set of operations, you can wrap them in Try / Catch block so if one set fail, you can perform additional logging and also decide whether or not the task should be cancelled altogether or continue through. (Note: When using try/catch, you'll want to set -ErrorAction Stop on the functions that have that parameter to make sure they get caught properly in the catch block.

References

Msdocs - Start-Transcript

Msdocs - Task scheduler -Security context for running task

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.