我需要脚本在特定时间运行,获取系统时间以确定打开哪个URL,截取屏幕截图,保存并通过邮件发送。 如果我打破代码,它的部分运行,但当我包括时间检查和if语句它不运行。 这是我提出的脚本,但它没有运行。
Function Get-shot {
$now = (Get-Date -Format "hh:mm:ss")
$ie=new-object -com "InternetExplorer.Application"
$ie.Visible=$true
$path="C:\checks"
Function takeshot {
$ScreenBounds = [Windows.Forms.SystemInformation]::VirtualScreen
$ScreenshotObject = New-Object Drawing.Bitmap
$ScreenBounds.Width,
$ScreenBounds.Height
$DrawingGraphics = [Drawing.Graphics]::FromImage($ScreenshotObject)
$DrawingGraphics.CopyFromScreen($ScreenBounds.Location, [Drawing.Point]::Empty, $ScreenBounds.Size)
$DrawingGraphics.Dispose()
$ScreenshotObject.Save($FilePath)
$ScreenshotObject.Dispose()
}
Try {
Add-Type -Assembly System.Windows.Forms
If ($now -eq "08:28:00")
{
<#opens internet explorer and navigates to the page#>
$ie.navigate("www.google.com")
$Time = (Get-Date)
[string] $FileName = "$($Time.Month)"
$FileName += '-'
$FileName += "$($Time.Day)"
$FileName += '-'
$FileName += "$($Time.Year)"
$FileName += '-'
$FileName += '.png'
[string] $FilePath = (Join-Path $Path $FileName)
#takes the screenshot
DO {
takeshot
Write-Verbose "Saved screenshot to $FilePath."
} while ($now -eq "08:28:30")
}
} Catch {
Write-Warning "$Error[0].ToString() +
$Error[0].InvocationInfo.PositionMessage"
}
}
答案 0 :(得分:0)
您没有说明它将在何处运行。含义:
您发布的代码中有一些不正确的内容。
警告:找不到构造函数。找不到合适的 Drawing.Bitmap类型的构造函数。找不到构造函数。 找不到类型Drawing.Bitmap的适当构造函数。您 无法在空值表达式上调用方法。你不能打电话给 null值表达式的方法。调用“FromImage”的异常 “1”参数:“值不能为空。
An for this ...
但是当我包括时间检查和if语句时它不会运行。
您的代码指定了明确的时间。
If ($now -eq "08:28:00")
如果不是那个确切的时间,当你运行代码时,代码将无法运行,因为你告诉它不要,如果它不是08:28:00“。
如果您在一天中运行此测试。将上述内容更改为此...
If ($now -ge "08:28:00")
然而,从那时起,这并没有真正成功。正如我之前所说,除非这个代码在某种类型的触发器上,WMI事件,Scheduled Task,已经通过无限循环在内存中运行(BTW不是一件好事,好吧,你可以设置一个定时循环的),然后它是反正不会开火。
演练(你必须围绕这个例子包含你的逻辑 - 包括任何多监视器的东西)
Clear-Host
$StartTime = $null
$TargetUrl = $null
$FileName = $null
$File = $null
# script to run at specific times
## Set up a schedueld task for the script run cycle(s)
### Scheduled Task (set script run daily at "08:28:00") )
### WMI Event Monitor (check for clock time of "08:28:00"))
### Load the script on boot up and use a timed infinite loop (Load at boot, Loop un time time of "08:28:00" - need to reload script each day to handle if use never reboots)
# determine which url to open
# How the URL is determined
$TargetUrl = "google.com"
# Get-Content -Path "D:\Temp\UrlList.txt" | Get-Random -Count 1
# $ResponseUrl = (Read-Host -Prompt "Enter the URL to use")
# Open the IE to the needed URL
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate($TargetUrl)
$ie.visible = $true
$ie.fullscreen = $False
While ($ie.Busy) {Start-Sleep -Milliseconds 100}
# take the screenshot and saves it
Function New-ScreenShot
{
[CmdletBinding()]
[Alias('nss')]
Param
(
# [Parameter(Mandatory = $true)]
[string]$Path = "$env:USERPROFILE\Pictures"
)
$FileName = "$($env:COMPUTERNAME)_$(get-date -f yyyyMMddHHmmss).bmp"
$File = "$Path\$FileName"
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)
Write-Output "Screenshot saved to:"
Write-Output $File
Start-Sleep -Seconds 1
Start-Process -FilePath mspaint -ArgumentList $File
}
New-ScreenShot
# send it via mail
# from the PoSH built-in help file
# Example 2: Send an attachment
Send-MailMessage -From "User01 <user01@example.com>" `
-To "User02 <user02@example.com>", "User03 <user03@example.com>" `
-Subject "Sending the URL Screenshot Attachment" `
-Body "Forgot to send the attachment. Sending now." `
-Attachments $File -Priority Low -dno onSuccess, onFailure -SmtpServer "smtp.example.com"
<强> 更新 强> 相对于OP评论
- athanas otieno
当然你可以做不同的类型。 我提供的是这样做的基础知识。有更多优雅的选择。 还有很多其他样本可以用来显示.png或.jpg用例。这些都在网络上,甚至在这个网站上。请参阅这些帖子。
如何在Windows PowerShell中执行屏幕捕获?
GET-Screenshot.ps1
<# .Synopsis Gets a screen capture .Description Captures the current screen .Example # Capture the whole screen Get-ScreenShot .Example # Capture the current window Get-ScreenShot -OfWindow .Example # Capture a set of coordinates Get-ScreenShot -OfCoordinates 320, 240 .Link http://stackoverflow.com/questions/2969321/how-can-i-do-a-screen-capture-in-windows-powershell #> # The image format used to store the screen capture [Parameter(ValueFromPipelineByPropertyName=$true)] [ValidateSet('PNG', 'JPEG', 'TIFF', 'GIF', 'BMP')] [string] $ImageFormat = 'JPEG', https://www.powershellgallery.com/packages/RoughDraft/0.1/Content/Get-Screenshot.ps1
Take-Screenshot这个脚本有一个功能,允许你采取 整个桌面或活动窗口的屏幕截图。还包括 将屏幕截图保存到文件的选项。
Take-ScreenShot -screen -file“C:\ image.png”-imagetype png
即使这个剧本似乎也是你的故事,但我可能是错的,因为通常很容易看到人们最终得到类似的东西。它发生在我身上很多次。当然,分叉/借用和调整是一回事。
将屏幕截图更改为JPG而非PNG
我有一个可以在计划任务上运行的脚本快照 电脑的屏幕。基本上,计算机在a处运行查询 定期间隔,在服务器上相当资源密集但是 拥有任何人可能希望在给定时间看到的信息。