简体   繁体   中英

PowerShell script lacks consistency when run through task scheduler, am I missing something or is this Windows being Windows?

I am a beginner in PowerShell.

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

This is my first time asking a question here, please forgive and point out mistakes in forum standards.

Thank You in advance!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM