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

我有一个PowerShell脚本来捕捉截图。 如果在PowerShell或ISE中运行它运行良好,采取屏幕捕获没有问题。 当我在Windows任务计划程序上安排任务时,它只保存一个空白图像而不是屏幕截图。 任何想法为什么?

脚本:

$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任务信息

常规选项卡:

  • 运行用户是否login
  • 以最高的权限运行

TRIGGERS TAB:

  • 每天上午8点运行,每30分钟运行12小时。

操作标签:

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

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

你的问题是,通过select“运行用户是否login”,你基本上是告诉任务运行在会议0,这是不是你login桌面上。

更详细的信息可以在这个technet博客文章:

帮帮我! 我的预定任务不运行…

它可能与执行脚本的用户的configuration文件有关。 当你从ise执行它的时候,你隐式地使用当前用户的configuration文件,你做当前屏幕的屏幕截图。

但是,当您的脚本从任务计划程序执行时,它将使用该任务的configuration文件中提到的configuration文件,并且可能没有与桌面打开的会话。 这是因为你有一个白色的图像,这个会话没有任何桌面。

您可以尝试更改将与已打开会话的用户一起执行的用户,并查看结果。可以手动启动任务以进行testing。