-
-
Save ahhh/25a8cb327ea689c8c0eaab6761191ee6 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(Mandatory = 0)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('Path', 'Out', 'o')] | |
[string]$OutPath = "$env:USERPROFILE\Documents\ScreenShot", | |
#screenshot_[yyyyMMdd_HHmmss_ffff].png | |
[parameter(Mandatory = 0)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('Name', 'n')] | |
[string]$FileNamePattern = 'screenshot_{0}.png', | |
[parameter(Mandatory = 0)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('Repeat', 'r')] | |
[int]$RepeatTimes = 0, | |
[parameter(Mandatory = 0)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('Duration','d')] | |
[int]$DurationMs = 1 | |
) | |
$script = { | |
Param ( | |
[Parameter(Position = 0)] | |
[string]$OutPath = "$env:USERPROFILE\Documents\ScreenShot", | |
[Parameter(Position = 1)] | |
[string]$FileNamePattern = 'screenshot_{0}.png', | |
[Parameter(Position = 2)] | |
[int]$RepeatTimes = 0, | |
[Parameter(Position = 3)] | |
[int]$DurationMs = 1 | |
) | |
$ErrorActionPreference = 'Stop' | |
Add-Type -AssemblyName System.Windows.Forms | |
if (-not (Test-Path $OutPath)) | |
{ | |
New-Item $OutPath -ItemType Directory -Force | |
} | |
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 | |
} | |
} | |
} | |
# Setup ScreenCaps's runspace | |
# To have it run in the background | |
$PowerShell = [PowerShell]::Create() | |
[void]$PowerShell.AddScript($Script) | |
[void]$PowerShell.AddArgument($OutPath) | |
[void]$PowerShell.AddArgument($FileNamePattern) | |
[void]$PowerShell.AddArgument($RepeatTimes) | |
[void]$PowerShell.AddArgument($DurationMs) | |
# Start ScreenCapture | |
[void]$PowerShell.BeginInvoke() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment