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
Start-Transcript -Path "Some\Path\AlarmLog_$(get-date -f 'yyyyMMdd').txt"/Stop-Transcriptat 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.Task Scheduler, what does theLast Run Resultcolumn show for that task after a failure? If you right-click the task and selectRundoes it always succeed?$pro = Get-Process -Name "winamp". All manual runs have failed tonight, leaving me withLast 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.