Powershell 脚本将运行但屏幕捕获未完成,为什么?

vee*_*l84 2 powershell scheduled-task task-scheduler

我有一个 powershell 脚本来捕获屏幕截图。如果在 powershell 或 ISE 中运行它,它运行良好,可以毫无问题地进行屏幕截图。当我在 Windows Task Scheduler 上安排任务时,它只会保存一个空白图像而不是屏幕截图。任何想法为什么?

脚本:

$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)
Run Code Online (Sandbox Code Playgroud)

Windows 任务信息

常规选项卡:

  • 无论用户是否登录都运行
  • 以最高权限运行

触发器选项卡:

  • 每天从早上 8 点开始每 30 分钟运行一次,持续 12 小时。

操作选项卡:

  • 启动程序:powershell.exe
  • 添加参数:-ExecutionPolicy 绕过“c:\path\script.ps1”

在我看来,脚本正在运行,但在通过 Windows 任务调度程序运行时没有捕获屏幕截图。保存的图像只是一页白纸。有谁知道为什么这不起作用?

Rya*_*ger 6

您的问题是,通过选择“无论用户是否登录都运行”,您基本上是在告诉任务在会话 0 中运行,而会话 0 不是您登录的桌面。

此技术网博客文章中提供了更详细的信息:

帮助!我的计划任务没有运行...