PowerShell 屏幕截图 [英] PowerShell Screen Capture
问题描述
我已经坚持了最后一天左右,我认为这是我看不到的小东西.
I have been stuck on this for last day or so and I think its something small that I just can't see.
总结一下代码:Powershell 脚本将每 60 秒运行一次以获取屏幕截图.在 Windows Server 2012 上,这工作得很好.在 Windows 7 上,只拍摄第一个屏幕截图,然后所有屏幕截图都是空白",只有尺寸大小的空白区域.任何想法为什么会发生这种情况?
To summarize the code: Powershell script that will run every 60 seconds to take a screen capture. On Windows Server 2012 this works perfectly fine. While on Windows 7, only first screen shot is taken and then all after are "blank" with just white space the size of dimensions. Any Ideas why this happens?
我还尝试删除较大的 while 循环并执行每 5 分钟运行一次的计划任务,但同样不起作用,我得到的只是空白图像.有什么想法吗?在 Windows 7 上.我使用的是本地管理员用户.
I also attempted to remove the bigger while loop and do a scheduled task that runs every 5 minutes, but again that doesn't work and all I get is a blank white image. Any thoughts? On windows 7. I am using local Admin user.
我在 windows 7 和 windows server 2012 上都使用 powershell 4.和 .Net 4.5
I am using powershell 4 across both windows 7 and windows server 2012. And .Net 4.5
while($true){
[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
function screenshot([Drawing.Rectangle]$bounds, $path) {
$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($path)
$graphics.Dispose()
$bmp.Dispose()
}
#NUC bounds
$bounds = [Drawing.Rectangle]::FromLTRB(0, -1080, 1920, 1080)
#remote display bounds
#$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1280, 800)
$PC_name=$(Get-WmiObject Win32_Computersystem).name
$dateandtime = Get-Date -Format yyyy-MM-dd-hh-mm-ss
$path_pcname= "C:\Scripts\Screenshots\" + $PC_name + "_screenshot_" + "$dateandtime"+ ".png"
screenshot $bounds $path_pcname
$limit = (Get-Date).AddMinutes(-15)
$path_todelete = "C:\Scripts\Screenshots\"
# Delete files older than the $limit.
Get-ChildItem -Path $path_todelete -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
Start-Sleep -Seconds 60
}
我意识到这一行:[Reflection.Assembly]::LoadWithPartialName("System.Drawing")从 GAC 读取的数据未正确启动.不确定这是否有帮助.
I realized that this line: [Reflection.Assembly]::LoadWithPartialName("System.Drawing") which reads from the GAC, isn't being initiated correctly. Not sure if this helps.
推荐答案
很酷的脚本.
为了让它在 Windows 2012R2 和带有 SP 1 的 Windows 7 Enterprise 以及都运行 PowerShell 4 上工作,我所做的唯一修改是 bounds 变量:
The only modification I made to get it working working, on both Windows 2012R2 and Windows 7 Enterprise with SP 1 and also both running PowerShell 4, is the bounds variable:
$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1600, 900) # My resolution.
效果很好.
现在,只是想出一个目的.
Now, just trying to think of a purpose for it.
感谢分享您的脚本.
这篇关于PowerShell 屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!