gpt4 book ai didi

powershell - 为什么我的屏幕截图功能无法正常工作

转载 作者:行者123 更新时间:2023-12-02 23:57:34 24 4
gpt4 key购买 nike

我在Job中有一个需要截图的函数。脚本本身由Taskscheduler作为域管理员执行。我还选中了“以最高特权运行”。

作业应该做一个屏幕截图,然后发送一封电子邮件,但是这两种情况都不会发生。我也没有看到错误消息(也许是因为它是后台作业?)。电子邮件可能没有发送,因为它想将屏幕截图附加到电子邮件中,但由于未创建屏幕快照而无法发送。

为什么我的功能无法拍摄截图?域管理员有权写入目标。当我在start-job之外运行函数时,将创建屏幕截图。如果我在start-job中具有该函数,则不会创建该函数,该脚本是通过Taskscheduler还是手动启动都没有关系。

我想念什么?

以下代码启 Action 业并截取屏幕截图:

Start-Job -Name LogikWebserverWatch {

function Take-Screenshot([string]$outfile)
{
[int]$PrtScrnWidth = (gwmi Win32_VideoController).CurrentHorizontalResolution
[int]$PrtScrnHeight = (gwmi Win32_VideoController).CurrentVerticalResolution
$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, $PrtScrnWidth, $PrtScrnHeight)
$bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
$graphics = [Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
$bmp.Save($outfile)
$graphics.Dispose()
$bmp.Dispose()
}

while ((Get-Process LogikWebserver).Responding) {sleep -Milliseconds 50}

if (!(Get-Process LogikWebserver).Responding) {
Try{

$utf8 = New-Object System.Text.utf8encoding
$datetime = (get-date).ToString('yyyyMMdd-HHmmss')
Take-Screenshot -outfile C:\Install\LogikWebserverErrorReporting\Screenshot-$datetime.png
# some more code [...]
} Catch { some more code [...] }
}}

最佳答案

The documentations说:

A Windows PowerShell background job runs a command without interacting with the current session.



因此,您可能必须先在作业中加载所需的程序集,然后它才能起作用。

当我在上面尝试您的代码时,它仅在作业之外运行时才创建了屏幕截图(如您所述),但是将这行添加到 Start-Job ScriptBlock的顶部也导致它也可以在作业内部运行:
[Reflection.Assembly]::LoadWithPartialName("System.Drawing")

或者,因为上面已经废止了:
[System.Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Drawing.dll")

要么
Add-Type "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Drawing.dll"

请注意,从计划任务运行时,我尚未对此进行测试。

关于powershell - 为什么我的屏幕截图功能无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40380011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com