0

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?

4

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…

  • Sir you deserve a medal! I had a similar idea in the back of my mind, that it was not taking the screenshot because the user that was running the script was not logged in. Your link solved my problem, I have my script working, i am running it as the logged in user and saving it locally, I have a seperate script that grabs the screen shot, cuts & pastes it to another location for storage. – veel84 Sep 6 '17 at 19:03
0

It maybe related with the profile of the user which execute the script. When you execute it from ise, you implicitly use the profile of the current user and you do the screenshot of the current screen.

But when your script is executed from task scheduler, it uses the profile mentioned in the conf of the task and may not have a opened session with a desktop. It for that you have a white image, there is not any desktop with this session.

You can try to change the user who will executes with a user who have an opened session and see the result.the task can be launch manually for test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.