Powershell Script will run but screen capture not completed, why

powershellscheduled-tasktask-scheduler

I have a powershell script to capture a screenshot. If Run it in powershell or ISE it runs fine, takes the screen capture without issues. When I schedule a task on windows Task Scheduler it just saves a blank image instead of the screen capture. Any ideas why?

Script:

$path = "\\somelocation\" 
$fileName = "Test"
$date = Get-Date -Format yyyyMMdd-hhmm
$file = $path + $filename + $date + ".bmp" 
Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing
# Gather Screen resolution information
$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$Width = $Screen.Width
$Height = $Screen.Height
$Left = $Screen.Left
$Top = $Screen.Top
# Create bitmap using the top-left and bottom-right bounds
$bitmap = New-Object System.Drawing.Bitmap $Width, $Height
# Create Graphics object
$graphic = [System.Drawing.Graphics]::FromImage($bitmap)
# Capture screen
$graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)
# Save to file
$bitmap.Save($File)

Windows Task info

GENERAL TAB:

  • runs whether user is logged on or not
  • runs with highest privileges

TRIGGERS TAB:

  • Runs daily starting at 8am every 30 minutes for 12 hours.

ACTIONS TAB:

  • Start a program: powershell.exe
  • add arguments: -ExecutionPolicy Bypass "c:\path\script.ps1"

It seems to me like the script is running but not capturing the screenshot when run through windows task scheduler. The saved image is just one white page. Does anyone know why this is not working?

Best Answer

Your problem is that by choosing "runs whether user is logged on or not", you're basically telling the task to run in session 0 which is not your logged on desktop.

More detailed info is available in this technet blog post:

Help! My Scheduled Task does not run…