我创建了一个PowerShell脚本,该脚本打开了一个位于Excel电子表格中的几个网站,并为每个网站制作了屏幕截图。
这里是代码:
[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()
}
$data = Import-Excel 'C:\Temp\HostNames\HostNames.xlsx'
$data | ForEach-Object {
$website = $_
$url=$website.Hostnames.Split(',')[0]
Write-Host $website.Name $url
$IE=new-object -com internetexplorer.application
$IE.visible=$true
$IE.FullScreen=$false
$IE.ToolBar = $false
$IE.StatusBar = $false
$IE.MenuBar = $false
$IE.AddressBar = $true
$IE.Resizable = $true
$IE.Top = 0
$IE.Left = 577
$IE.Width = 1024
$IE.Height = 747
$IE.navigate2( $url )
$i=0
While ( $IE.busy -eq $true ) {
Start-Sleep -s 1
$i = $i + 1
if ( $i -ge 20 ) { break }
}
$bounds = [Drawing.Rectangle]::FromLTRB($IE.Left, $IE.Top, $IE.Left + $IE.Width, $IE.Top + $IE.Height)
$filename = "C:\Temp\HostNames\urlshots\"+ ($website.Name) +".png"
Screenshot $bounds $filename
$IE.Quit()
}
问题是在运行每个网站后,我一直收到此错误:
Exception calling "Save" with "1" argument(s): "A generic error occurred in GDI+."
At C:\Temp\HostNames\HostNames View.ps1:9 char:4
+ $bmp.Save($path)
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ExternalException
任何想法都很棒!
尝试一下...(如Theo所指)
我只是做了一些调整以支持我,因为我不想做所有出色的工作。
取决于计算机屏幕上的内容,您还必须同时从浏览器进入浏览器,因此,我的调整将其添加。
[void] [Reflection.Assembly]::LoadWithPartialName('System.Drawing')
[void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
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()
}
# Open the browser once
$IE = New-Object -ComObject internetexplorer.application
$IE.visible = $true
$IE.FullScreen = $false
$IE.ToolBar = $false
$IE.StatusBar = $false
$IE.MenuBar = $false
$IE.AddressBar = $true
$IE.Resizable = $true
$IE.Top = 0
$IE.Left = 577
$IE.Width = 1024
$IE.Height = 747
$i = 0
While ( $IE.busy -eq $true )
{
Start-Sleep -Seconds 1
$i = $i + 1
if ( $i -ge 20 )
{ break }
}
# Read and process the data in the file
# Import-Excel -Path 'C:\Temp\HostNames\HostNames.xlsx'
# Set focus on the browser
[Microsoft.VisualBasic.Interaction]::AppActivate('Internet Explorer')
'microsoft.com','google.com','stackoverflow.com' |
ForEach-Object {
<#
$website = $_
# Open an new site in a new tab, set focus on the tab
$url = $website.Hostnames.Split(',')[0]
#>
"Processing the website names $PSItem"
$IE.navigate2( $PSItem )
$bounds = [Drawing.Rectangle]::
FromLTRB($IE.Left, $IE.Top, $IE.Left + $IE.Width, $IE.Top + $IE.Height)
$filename = "D:\Temp\$($PSItem -replace '.com').png"
# Take a screenshot
Screenshot $bounds $filename
Start-Sleep -Seconds 3
}
# Release resources
$IE.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($IE) |
Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Get-ChildItem -Path 'D:\temp' -Filter '*.png'
<#
# Results
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05-Mar-20 11:45 172410 google.png
-a---- 05-Mar-20 11:45 17781 microsoft.png
-a---- 05-Mar-20 11:45 50634 stackoverflow.png
#>