-
-
Save pogin503/53b5f0b0f60282b6f3d040d0735a3e0d to your computer and use it in GitHub Desktop.
Screenshot Automation with PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ScreenShot | |
{ | |
[CmdletBinding()] | |
param( | |
[parameter(Position = 0, Mandatory = 0, ValueFromPipelinebyPropertyName = 1)] | |
[ValidateNotNullOrEmpty()] | |
[string]$OutPath = "$env:USERPROFILE\Documents\ScreenShot", | |
#screenshot_[yyyyMMdd_HHmmss_ffff].png | |
[parameter(Position = 1, Mandatory = 0, ValueFromPipelinebyPropertyName = 1)] | |
[ValidateNotNullOrEmpty()] | |
[string]$FileNamePattern = 'screenshot_{0}.png', | |
[parameter(Position = 2,Mandatory = 0, ValueFromPipeline = 1, ValueFromPipelinebyPropertyName = 1)] | |
[ValidateNotNullOrEmpty()] | |
[int]$RepeatTimes = 0, | |
[parameter(Position = 3, Mandatory = 0, ValueFromPipelinebyPropertyName = 1)] | |
[ValidateNotNullOrEmpty()] | |
[int]$DurationMs = 1 | |
) | |
begin | |
{ | |
$ErrorActionPreference = 'Stop' | |
Add-Type -AssemblyName System.Windows.Forms | |
if (-not (Test-Path $OutPath)) | |
{ | |
New-Item $OutPath -ItemType Directory -Force | |
} | |
} | |
process | |
{ | |
0..$RepeatTimes ` | |
| %{ | |
$fileName = $FileNamePattern -f (Get-Date).ToString('yyyyMMdd_HHmmss_ffff') | |
$path = Join-Path $OutPath $fileName | |
$b = New-Object System.Drawing.Bitmap([System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width, [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height) | |
$g = [System.Drawing.Graphics]::FromImage($b) | |
$g.CopyFromScreen((New-Object System.Drawing.Point(0,0)), (New-Object System.Drawing.Point(0,0)), $b.Size) | |
$g.Dispose() | |
$b.Save($path) | |
if ($RepeatTimes -ne 0) | |
{ | |
Start-Sleep -Milliseconds $DurationMs | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment